1 Answers
What is the difference between primary key and unique key constraints in DB2?
In DB2, the primary key and unique key constraints serve the purpose of ensuring data integrity within a database. The focus keyword here is difference between primary key and unique key constraints in DB2.
Primary Key Constraint:
- A primary key constraint uniquely identifies each record in a table.
- It enforces entity integrity by ensuring that no duplicate values are allowed in the primary key column.
- There can be only one primary key constraint per table.
- By default, a primary key constraint also enforces a NOT NULL constraint on the specified column(s).
Unique Key Constraint:
- A unique key constraint also ensures that each value in the specified column(s) is unique.
- Unlike the primary key constraint, a unique key constraint allows null values, meaning it can contain multiple NULL values (except for columns participating in the unique key).
- There can be multiple unique key constraints per table.
- Unique key constraints are used to enforce entity integrity but do not automatically enforce NOT NULL constraints.
Overall, the primary key constraint is stricter in terms of data uniqueness and does not allow NULL values, while the unique key constraint allows NULL values but maintains data integrity by ensuring uniqueness.
Please login or Register to submit your answer