Q3. What is the difference between a primary key and a unique key?
A primary key uniquely identifies each row and cannot contain NULL; a table has exactly one. A unique key also enforces uniqueness but allows a single NULL value, and a table can have several.
By default SQL Server backs a primary key with a clustered index and a unique constraint with a nonclustered index, though you can change that. Both prevent duplicate values in the columns they cover.
| Primary key | Unique key | |
|---|---|---|
| NULLs allowed | No | Yes, one |
| Per table | One | Many |
| Default index | Clustered | Nonclustered |
| Purpose | Row identity | Enforce uniqueness |
Key point: The follow-up is usually about NULLs. Say the primary key rejects NULLs while a unique key permits exactly one.