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

1 Answers
Answered by suresh

Primary Key vs Unique Key in RDBMS

Difference between Primary Key and Unique Key in RDBMS

In a Relational Database Management System (RDBMS), the primary key and unique key are both used to ensure the uniqueness of data values in a table, but they have distinct differences:

Primary Key:

  • A Primary Key is a column (or a set of columns) that uniquely identifies each row in a table.
  • It ensures that no two rows in the table have the same values for the primary key column(s).
  • By default, a primary key column cannot contain NULL values.
  • Each table can have only one primary key.
  • Primary key constraints automatically create a unique index on the primary key column(s).

Unique Key:

  • A Unique Key is a constraint that ensures all values in a column (or a set of columns) are unique.
  • Unlike a primary key, a unique key column can contain NULL values, but only one NULL is allowed.
  • Each table can have multiple unique keys.
  • Unique key constraints also create a unique index on the unique key column(s).
  • Unique keys can be used to enforce the uniqueness of values that are not intended to be primary keys.

Overall, the primary key uniquely identifies each row in a table and is used for data integrity and relational integrity, while the unique key enforces uniqueness on a column or set of columns and helps in maintaining data consistency.

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