+ 1
What is the difference between UNIQUE and PRIMARY KEY?
2 Answers
+ 3
PRIMARY KEY(PK) can be set to only 1 column whereas UNIQUE can be set across multiple columns in a table. Also, UNIQUE constraint accepts NULL values whereas PK doesn't.
0
Primary Key
Primary key cannot have a NULL value.
Each table can have only one primary key.
By default, Primary key is clustered index, and the data in database table is physically organized in the sequence of clustered index.
Primary key can be related to another tables as a Foreign Key.
We can generate ID automatically with the help of Auto Increment field. Primary key supports Auto Increment value.
We can define Primary key constraint on temporary table and table variable.
We can't delete primary key value from the parent table which is used as a foreign key in child table. To delete we first need to delete that primary key value from the child table.
Unique Key
Unique Constraint may have a NULL value.
Each table can have more than one Unique Constraint.
By default, Unique key is a unique non-clustered index.
Unique Constraint can not be related with another table's as a Foreign Key.
Unique Constraint doesn't supports Auto Increment value.