Difference between Unique key and Primary key
A UNIQUE constraint and PRIMARY key both are similar and it provide unique enforce uniqueness of the column on which they are defined.
Some are basic differences between Primary Key and Unique key are as follows.
Primary key
- Primary key cannot have a NULL value.
- Each table can have only single primary key.
- Primary key is implemented as indexes on the table. By default this index is clustered index.
- Primary key can be related with another table's as a Foreign Key.
- We can generated ID automatically with the help of Auto Increment field. Primary key supports Auto Increment value.
- Unique Constraint may have a NULL value.
- Each table can have more than one Unique Constraint.
- Unique Constraint is also implemented as indexes on the table. By default this index is 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.
Create
table Student
(
StuId
int primary
key, ----
Define Primary Key
StuName
varchar(50)
Not Null,
ContactNo
int Unique
--- Define Unique Key
)
Insert some data in table
Now you will see above fig. StuId can not have NULL value but ContactNo can have NULL value.