Primary key Vs Unique key


Primary Key

Primary Key is a column that is used to uniquely identify each tuple of the table.

It is used to add integrity constraints to the table. Only one primary key is allowed to be used in a table. Duplicate and NULL (empty) values are not valid in the case of the primary key. Primary keys can be used as foreign keys for other tables too.

Let’s take an example,

We have a table name employee which stores data of employees of a company. The below table shows the contents of the table.

Emp_idNamePh_No.PositionSalary

Emp_id here is primary key of the table. As the id of each employee is unique and no two employees can have the same Emp_id.

Unique Key

Unique key is a constraint that is used to uniquely identify a tuple in a table.

Multiple unique keys can present in a table. NULL values are allowed in case of a unique key. These can also be used as foreign keys for another table.

Let’s take an example,

We have a table name employee which stores data of employees of a company. The below table shows the contents of the table.

Emp_idNamePh_No.PositionSalary

Ph_No here is the foriegn key of the table. As the phone number of each employee is unique and it might be possible that an employee does not have any phone number.

Difference between Primary Key and Unique Key 

Primary KeyUnique Key
Unique identifier for rows of a tableUnique identifier for rows of a table when primary key is not present
Cannot be NULLCan be NULL
Only one primary key can be present in a tableMultiple Unique Keys can be present in a table
present in a tablepresent in a table
Selection using primary key creates clustered indexSelection using unique key creates non-clustered index

Updated on: 03-Feb-2020

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements