What are the differences between a primary key and a unique key in a relational database management system (RDBMS)?

1 Answers
Answered by suresh

Differences between Primary Key and Unique Key in RDBMS

Differences between Primary Key and Unique Key in RDBMS

In a Relational Database Management System (RDBMS), both primary key and unique key are used to enforce uniqueness of data. However, there are key differences between them:

Primary Key

  • Primary key is a column (or a set of columns) that uniquely identifies each record in a table.
  • A table can have only one primary key.
  • Primary key does not allow null values.
  • Primary key automatically creates an index on the column(s) it is defined on.
  • Primary key is used to establish relationships between tables.

Unique Key

  • Unique key is a column (or a set of columns) that ensures the values in the column(s) are unique.
  • A table can have multiple unique keys.
  • Unique key allows null values, except when the column(s) are part of a composite unique key.
  • Unique key creates a unique constraint that ensures data integrity.
  • Unique key is used to enforce data uniqueness but not as a reference for foreign key relationships.

Overall, the primary key is used to uniquely identify records and establish relationships, while unique key is used to ensure data integrity and enforce uniqueness constraints in a table.

Answer for Question: What are the differences between a primary key and a unique key in a relational database management system (RDBMS)?