Can you explain the difference between a primary key and a unique key in a database?

1 Answers
Answered by suresh

Primary Key vs Unique Key in Database | Database Administration Interview Question

Primary Key vs Unique Key in Database

When it comes to database administration, understanding the difference between a primary key and a unique key is essential. Here's a simple explanation:

Primary Key

A primary key is a column or a set of columns that uniquely identify each record in a table. It must be unique for each record and cannot contain null values. A table can have only one primary key.

Unique Key

A unique key is a constraint that ensures the values in a column or a set of columns are unique across the table. Unlike a primary key, a unique key can allow null values, but it can only have one null value. A table can have multiple unique keys.

In summary, a primary key uniquely identifies each record, while a unique key ensures that each value in a column is unique. Both are important for maintaining data integrity in a database.

Answer for Question: Can you explain the difference between a primary key and a unique key in a database?