Difference Between Primary Key and UNIQUE Constraint

Primary Key and Unique Constraint , both are used in relational database to ensure uniqueness but they are some different/unique aspects in both. Lets discuss some of these aspects :-

  1. Primary key do not allows NULL values but on the other hand UNIQUE constraint allows NULL value.
  2. We can have only one primary key in our table, on the other hand we can have more than one UNIQUE columns in our table.

Null Values in UNIQUE Columns

UNIQUE columns cannot accepts duplicate values however it can accept more than one null values. It has shown in the below example.

INSERT INTO w3wiki(user_id,name,rank,overall_score,monthly_score)
VALUES (205,'Harsh',NULL,1900,20);

INSERT INTO w3wiki(user_id,name,rank,overall_score,monthly_score)
VALUES (200,'Karan',NULL,1905,10);

Output:

w3wiki-table01

SQLite UNIQUE Constraint

SQLite is a lightweight relational database management system (RDBMS). It requires minimal configuration and it is self-contained. It is an embedded database written in C language. It operates a server-less, file-based database engine making it a good fit for mobile applications and simple desktop applications. It supports standard SQL syntax. In this article, we are going to cover all the necessary points of UNIQUE constraint. This article will cover every possible use of UNIQUE constraints.

Similar Reads

UNIQUE Constraint

UNIQUE constraints in SQLite ensure that a column or group of columns should not accept duplicate values. Simply, it prevents us from entering duplicate values in a column or group of columns....

Examples of UNIQUE Constraint

Let see some examples of unique constrain....

Difference Between Primary Key and UNIQUE Constraint

Primary Key and Unique Constraint , both are used in relational database to ensure uniqueness but they are some different/unique aspects in both. Lets discuss some of these aspects :-...

Conclusion

Unique Constraint is a crucial part of relational database system (RDBMS). It gives us flexibility to make our columns to accept only unique values. However, unlike primary key we can add NULL values in our Unique columns. We can also have more than one unique columns in our table. Unique constraint ensures us that our columns should not accept any duplicate values....

Contact Us