PRIMARY KEY against UNIQUE — Aptitude Question
PRIMARY KEY against UNIQUE: What distinguishes a PRIMARY KEY constraint from a UNIQUE constraint? Answer and worked solution on CodeKairo.
- Topic: OS, DBMS & Networks
- Section: Programming MCQs
- Difficulty: Easy
- Time target: 60 seconds
Question
What distinguishes a PRIMARY KEY constraint from a UNIQUE constraint?
Options
- A UNIQUE column may hold NULL, a PRIMARY KEY column may not
- A PRIMARY KEY column may hold NULL, a UNIQUE column may not
- Both allow NULL values freely
- Neither allows NULL, and a table may have only one of each
Show the answer and the worked solution
Correct answer: A. A UNIQUE column may hold NULL, a PRIMARY KEY column may not
Worked solution
- Both constraints reject duplicate values, and a table may carry several UNIQUE constraints but only one primary key.
- A primary key identifies a row, so its columns are implicitly NOT NULL.
- A UNIQUE constraint carries no such rule, so A UNIQUE column may hold NULL, a PRIMARY KEY column may not.
Answer: A UNIQUE column may hold NULL, a PRIMARY KEY column may not.
The idea
Entity integrity is the rule at work: no part of a primary key may be null, because a null cannot identify anything. UNIQUE only promises no two non-null values repeat.
Shortcut. PRIMARY KEY = UNIQUE + NOT NULL, and only one option matches that equation.
Common trap. "Neither allows NULL, and a table may have only one of each" tempts because both reject duplicates, but UNIQUE accepts NULL and a table may carry several UNIQUE constraints.