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

1 Answers
Answered by suresh

Difference between Primary Key and Unique Constraint in DB2

Difference between Primary Key and Unique Constraint in DB2

Primary Key and Unique Constraint are both used to enforce uniqueness in columns in a table in DB2, but there are some key differences between them:

  1. Primary Key:
    • Unique identifier for each row in a table.
    • Can consist of one or multiple columns.
    • Automatically creates a unique index on the primary key columns.
    • By default, primary key columns cannot contain NULL values.
    • Only one primary key constraint is allowed per table.
  2. Unique Constraint:
    • Ensures that all values in a column or a group of columns are unique.
    • Does not automatically create an index on the unique constraint columns.
    • Allows NULL values in the unique constraint columns, except for when the column is defined as NOT NULL.
    • Multiple unique constraints can be defined on a single table.

In summary, while both primary key and unique constraint enforce uniqueness in DB2, primary key is typically used to uniquely identify each row in a table, whereas unique constraint is used to enforce uniqueness on specific columns or groups of columns.

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