What is the difference between primary key and unique key in DB2?

1 Answers
Answered by suresh

What is the difference between primary key and unique key in DB2?

In DB2, both primary key and unique key are used to enforce uniqueness on a set of columns in a table. However, there are some key differences between the two:

Primary Key

  • A primary key uniquely identifies each row in a table and does not allow NULL values.
  • There can be only one primary key constraint per table.
  • Primary key automatically creates a unique index to enforce uniqueness.
  • Primary key can be referenced by foreign keys in other tables.

Unique Key

  • A unique key constraint enforces uniqueness on a set of columns, but it allows NULL values.
  • There can be multiple unique key constraints on a table.
  • Unique key automatically creates a unique index to enforce uniqueness.
  • Unique key can also be referenced by foreign keys in other tables.

In summary, primary key and unique key in DB2 serve similar purposes of enforcing uniqueness, but the primary key uniquely identifies rows and does not allow NULL values, while the unique key allows NULL values and can be applied to multiple columns in a table.

Answer for Question: What is the difference between primary key and unique key in DB2?