Keys: Superkey, Candidate, Primary & Foreign
A key is a promise: no two rows share this value, and the database will enforce it.
In plain English
A key is a name tag that no one else in the room can have.
The one idea
A key is a minimal set of attributes that determines every tuple; a foreign key is a reference to a key in another relation.
Keys are both the identity of a row and the anchor for joins. A schema without keys cannot distinguish two otherwise identical facts.
By the end
- 01Define superkey, candidate key, primary key, alternate key, and foreign key.
- 02Find candidate keys in a small relation.
- 03Explain referential integrity.
- 04Know the difference between a key and an index.
Superkeys and candidate keys
A superkey is any set of attributes that uniquely identifies a tuple. A candidate key is a minimal superkey: removing any attribute breaks uniqueness. A relation can have several candidate keys. The primary key is the chosen candidate key; the others are alternate keys.
Student(id, email, name)
id -> unique
email -> unique
(id, name) -> superkey but not minimal
candidate keys: {id}, {email}
primary key: id
alternate key: emailForeign keys
A foreign key is a set of attributes in one relation that references a candidate key in another relation. It enforces referential integrity: every non-null foreign-key value must match an existing referenced tuple. Foreign keys are how relationships are represented in the relational model.
Keys versus indexes
A key is a logical constraint. An index is a physical access structure. A primary key is usually indexed, but an index does not make a column unique unless it is declared unique. Confusing the two leads to schemas that look correct but allow duplicates.
The key of an enrollment
Enrollment contains student_id, course_id, grade, and student_name.
Test student_id alone.
It is not a key: one student can enroll in several courses.
Test (student_id, course_id).
It is unique and minimal, so it is a candidate key.
Add grade to the key.
It is still unique, but no longer minimal; it is a superkey, not a candidate key.
ConclusionA primary key is the chosen candidate key. A foreign key must match an existing key value or be null when the relationship is optional.
The trade-off
No free correctness.
Gain
Identity, integrity, and efficient joins.
Cost
Natural keys can change; surrogate keys add a column and require discipline.
Verdict
Use a stable surrogate key when the natural key may change; keep a unique constraint on the natural key.
Recap
What to keep.
Takeaways
- +A superkey uniquely identifies a tuple; a candidate key is minimal.
- +The primary key is the chosen candidate key; the rest are alternate keys.
- +A foreign key references a candidate key in another relation.
- +Referential integrity requires foreign keys to match existing rows.
- +A key is a constraint, not an index.
Traps
- !Calling every unique column a primary key.
- !Forgetting that a foreign key can be NULL unless declared NOT NULL.
- !Assuming an index enforces uniqueness.
Focus
- →Find all candidate keys in a small relation.
- →Distinguish primary, alternate, and foreign keys.
- →Explain referential integrity with an example.
Check yourself
Before you turn the page.
What is a candidate key?
What does a foreign key enforce?