What is the difference between a primary key and a unique key in SQL?
a) A primary key enforces uniqueness and cannot be NULL, a unique key allows NULL values
b) A primary key can be duplicated, a unique key cannot
c) A primary key is only used for foreign key relationships, a unique key is not
d) A primary key allows NULL values, a unique key does not
Answer:
a) A primary key enforces uniqueness and cannot be NULL, a unique key allows NULL values
Explanation:
A primary key is a column or a set of columns that uniquely identifies each row in a table and cannot contain NULL values. There can only be one primary key per table, and it ensures that each record is unique and easily identifiable.
A unique key also enforces uniqueness but allows NULL values. A table can have multiple unique keys. While the unique key ensures that the non-NULL values are distinct, it does not require all values to be present (i.e., NULL values are allowed).
Both primary keys and unique keys are used to enforce data integrity, but the primary key is typically used for establishing relationships between tables (via foreign keys).