- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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_id | Name | Ph_No. | Position | Salary |
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_id | Name | Ph_No. | Position | Salary |
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 Key |
---|---|
Unique identifier for rows of a table | Unique identifier for rows of a table when primary key is not present |
Cannot be NULL | Can be NULL |
Only one primary key can be present in a table | Multiple Unique Keys can be present in a table |
present in a table | present in a table |
Selection using primary key creates clustered index | Selection using unique key creates non-clustered index |
To Continue Learning Please Login
Login with Google