What is the difference between a primary key and a unique key in a database table?

1 Answers
Answered by suresh

Difference Between Primary Key and Unique Key in Database Table

Difference Between Primary Key and Unique Key in a Database Table

The main difference between a primary key and a unique key in a database table lies in their functionalities and constraints.

Primary Key

A primary key is a column or a set of columns that uniquely identify each row in a table. It enforces data integrity by ensuring that each value in the key is unique and not null. There can only be one primary key defined for a table.

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 allows one null value, meaning it does not enforce the not null constraint. Multiple unique keys can be defined in a single table.

Overall, while both primary keys and unique keys serve the purpose of enforcing data uniqueness, the primary key also acts as the identifier for each row in a table, whereas a unique key simply enforces uniqueness without necessarily identifying the row uniquely.

Answer for Question: What is the difference between a primary key and a unique key in a database table?