Home / Questions / Differentiate between primary key constraint and unique constraint. Why is unique constraint preferred over primary key constraint?
Explanatory Question

Differentiate between primary key constraint and unique constraint. Why is unique constraint preferred over primary key constraint?

👁 536 Views
📘 Detailed Answer
🕒 Easy to Read
Read the answer carefully and go through the related questions on the right side to improve your understanding of this topic.

Answer with Explanation

The primary key constraint ensures that the attributes, which are declared as primary keys must be unique and not null. The primary key constraint enforces entity integrity for the relation and constrains the relation in the following ways:

  • The primary key attribute must have unique set of values for all the tuples in a relation.
  • The primary key attribute cannot have null value.
  • A relation can have only one primary key attribute.

The unique constraint ensures that a particular set of attributes contains unique values and hence, two tuples cannot have duplicate values in specified attributes. Like primary key, it also enforces entity integrity.

Unique constraint is preferable over primary key constraint to enforce uniqueness of a non-primary key attribute. This is because

  • Multiple unique constraints can be applied on a relation whereas only one primary key constraint can be defined on a relation.
  • Unlike primary key constraint, unique constraint allows the attribute value to be null.