In this article we will discuss about what is the difference between primary key and unique key in SQL server.
In my last articles we have discussed about:
- Left outer join and Right outer join in sql server
- constraints in sql server
- Implement delete functionality in Gridview in Asp.Net
Unique key:
-There may be more than one unique key can be present inside a table.
-It supports null values but only once.
-But it doesn't support duplicates like suppose inside column there are two null values then Sql Server internally give some different values but values are not equal.
-Unique key constraint is used to prevent the duplication of key values within the rows of a table and allow null values.
- By default Unique Key will Generate Non-Clustured Index.
Primary key:
-Primary key is(Unique key+not null). Primary Key is a combination of Unique and NOT NULL Constraints so it can’t have duplicate values or any NUll.
-There must be one primary key can be present inside a table.
-It doesn't support null values .
-It doesn't support duplicates.
-Primary key allows each row in a table to be uniquely identified and ensures that no duplicate rows exist.
- By default Primary Key will generate Clustured Index.
In my last articles we have discussed about:
- Left outer join and Right outer join in sql server
- constraints in sql server
- Implement delete functionality in Gridview in Asp.Net
Unique key:
-There may be more than one unique key can be present inside a table.
-It supports null values but only once.
-But it doesn't support duplicates like suppose inside column there are two null values then Sql Server internally give some different values but values are not equal.
-Unique key constraint is used to prevent the duplication of key values within the rows of a table and allow null values.
- By default Unique Key will Generate Non-Clustured Index.
Primary key:
-Primary key is(Unique key+not null). Primary Key is a combination of Unique and NOT NULL Constraints so it can’t have duplicate values or any NUll.
-There must be one primary key can be present inside a table.
-It doesn't support null values .
-It doesn't support duplicates.
-Primary key allows each row in a table to be uniquely identified and ensures that no duplicate rows exist.
- By default Primary Key will generate Clustured Index.