Monday, 2 January 2012

What is the difference between primary key and unique key?

Primary key and unique are Entity integrity constraints

primary key doesnot accept the duplicate values and null values. the unique key doesnot accept the duplicate value but it accept the null values.
All Primary Key is Unique,but unique key is not a primary key.
Primary key is a column or set of columns which uniquely identifies a row in a table.There must be only one primary key for each table.It does not contains any null values.It also does contains any duplicate values.All the values in the primary keycolumn must be unique.It is a unique key.

Unique key is a constraint which is defined on a column in a table to avoid duplicatevalues in that column.But it allows one null value
In SQL Server, you can create a table with multiple unique key but you can't create a table with multiple primary key
Primary Key will create the clustered index in a table. Uniqe key will create the non-clustered index in a table
Primary Key automatically Create Cluster Index While Unqiue key doesn't create.

No comments:

Post a Comment