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

1 Answers
Answered by suresh

Difference between Primary Key and Unique Key in DB2

Primary Key vs Unique Key in DB2

Primary Key and Unique Key are both used in DB2 to enforce data integrity and ensure uniqueness of values in a table. The main differences between them are:

  1. Primary Key:
    • There can be only one Primary Key defined per table in DB2.
    • Primary Key does not allow NULL values.
    • Primary Key automatically creates a unique index on the specified column(s).
    • Can be referenced by foreign keys from other tables.
  2. Unique Key:
    • Multiple Unique Key constraints can be defined per table in DB2.
    • Unique Key allows NULL values, but only one NULL value is allowed (except in columns defined as NOT NULL).
    • Unique Key also creates a unique index on the specified column(s).
    • Unique Key values do not have to be used in foreign key relationships.

It is important to choose between Primary Key and Unique Key based on the specific requirements of your database schema to ensure data consistency and integrity.

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