Verified Jobs Live
Database Easy Level ~3 min read

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.
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.