How do a primary key, foreign key, and unique constraint serve different purposes?
Interactive Practice Mode
Formulate your thoughts or take notes before revealing the model solution.
Saved automatically
Model Answer & Deep-Dive Explanation
A primary key identifies each row and is non-NULL. A foreign key enforces an allowed relationship to a referenced key, helping prevent orphaned data. A unique constraint prevents duplicate combinations according to the database’s rules, with NULL handling varying by system and configuration. A table may have multiple candidate unique keys but only one declared primary key. Choose stable identifiers and explicit referential actions. Application validation improves messages, but database constraints protect integrity across every writer, including concurrent requests and maintenance scripts.
Related Questions in Database
DBA Interview Questions
Intermediate level • 827 views
MS-SQL Server developer interview questions
Advanced level • 341 views
MySQL management interview questions
Beginner level • 248 views
How would you model a many-to-many relationship between students and courses?
Medium level • 81 views
What is normalization, and when might denormalization be justified?
Easy level • 79 views