2 min read

Primary key Vs Unique key

Keys play an important role in the relational database. It is used to uniquely identify any record or row of data from the table. It is also used to establish and identify relationships between tables.
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 Key

-->Unique identifier for rows of a table.

-->Cannot be NULL.

-->Only one primary key can be present in a table.

Unique Key

-->Unique identifier for rows of a table when primary key is not present.

-->Can be NULL.

-->Multiple Unique Keys can be present in a table.

Primary key Vs Unique key
Primary KeyPrimary 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 prim ...
https://www.tutorialspoint.com/primary-key-vs-unique-key

#PrimaryKey #UniqueKey #Probyto #ProbytoAI

Subscribe and follow us for latest news in AI and Tech and stay updated!

Facebook: https://facebook.com/probyto
Twitter: https://twitter.com/probyto
LinkedIn: https://linkedin.com/company/probyto
Instagram: https://instagram.com/probyto