What is difference between UNIQUE and PRIMARY KEY constraints?

A table can have only one PRIMARY KEY whereas there can be any number of UNIQUE keys. The columns that compose PK are automatically define NOT NULL, whereas a column that compose a UNIQUE is not automatically defined to be mandatory must also specify the column is NOT NULL.

• Sep 20th, 2005

Hi this is modified answer ,pls post this instead of previous which i post

Primay Key                     Unique key

-----------                  ------------------

There is only one         there may be more than 1
Primary key for           Unique Key in table
1 table

It can not contain           It Can contain Null Value
Null value

Vijay Raj Jaiswal

• Sep 20th, 2005

Hi this is modified answer ,pls post this instead of previous which i post

Primay Key                     Unique key

-----------                  ------------------

There is only one         there may be more than 1
Primary key for           Unique Key in table
1 table

It can not contain           It Can contain Null Value
Null value

kalai

• Sep 28th, 2005

Primary key creates clustered index whereas unique key creates non clustered index.primary key won't allow NULL values whereas unique key allows NULL values.

sugapriyaraja

• Oct 23rd, 2006

Primary Key: only one column in a table,not accepted null values.

Unique Key: More then one column in a table, null values accepted

bala

• Mar 1st, 2007

Primay Key                     Unique key

-----------                  ------------------

There is only one         there may be more than 1
Primary key for           Unique Key in table
1 table

It can not contain           It Can contain one Null Value
Null value

amit

• May 23rd, 2007

Primary key creates clustered index whereas unique key creates non clustered index. Primary key won't allow NULL values whereas unique key allows only one NULL values.

Faizal

• Jul 6th, 2007

 /*Just adding to the Primary Key Explanation*/Its right that there can be only one Primary Key for a Table.But for that Primary Key we can add more than one fields.Ex:ALTER TABLE emp ADD CONSTRAINT PRIMARY KEY(emp_no, dept_no, phone);By the above, all the 3 fields make a combination of a Primary Key.

• Aug 7th, 2007

Primary key wont accept null values and a table can have only one primary key for a column.
Unique key accepts null values.A table can have any number of unique keys.

Rahul Kataria

• Oct 19th, 2007

Primary key cannot take null values but Unique can take null values

simply we can say

primary key = unique + not null

• Jan 29th, 2008

unique key can contains one null value
primery key can not contain null value

• May 29th, 2008

A table can have only one PRIMARY KEY where as can have numerous UNIQUE KEY . A PRIMARY KEY is a unique KEY with the extra constraint that all key
columns must be defined as NOT NULL. But unique key column can contain null value only once. If UNIQUE KEY is defined on more than one column then null values can be in the table by unique combination of those columns. Another difference is that PRIMARY KEY constraints enforces clustered indexing where as UNIQUE KEY constrains are nonclustered indexes.

• Sep 16th, 2008

Primay Key                     Unique key

-----------                  ------------------

There is only one         there may be more than 1
Primary key for           Unique Key in table
1 table

It can not contain           It Can contain Null Value
Null value

Primary key                      unique key creates non clustered index
creates clustered
index

• Apr 30th, 2010

Unique key means we have to enter unique value but we can enter null.
But in case of Primary key we should have to enter Unique + Not Null value in that particular column.

Lakshmanan

• Jun 30th, 2017

Primary key is combination of not null and unique. Primary key does not insert a null value and insert value is unique. Unique is one time accepted null value and insert a data is uniquely

Send   Reset