4.4.2 · D3Databases

Worked examples — Keys — primary, candidate, super, foreign, natural vs surrogate

3,517 words16 min readBack to topic

The scenario matrix

Every question about keys is really one of these case classes. Think of it like the quadrants of a graph: we must visit each one.

# Case class The question being stressed Covered by
C1 Single unique column Is one attribute enough to be a key? Ex 1
C2 Composite (multi-column) key When is a bundle the smallest key? Ex 2
C3 Super vs candidate trimming Which supersets get disqualified? Ex 3
C4 Degenerate: duplicate values A "key" that isn't unique — what breaks? Ex 4
C5 Degenerate: NULLs present Can a column with blanks be a primary key? Ex 5
C6 Foreign key repetition / NULL May the FK column repeat or be empty? Ex 6
C7 Limiting case: whole-row key What if only all columns together are unique? Ex 7
C8 Natural vs surrogate choice Real-world word problem — which to pick? Ex 8
C9 Exam twist: counting super keys Given candidate keys, how many super keys exist? Ex 9
C10 Exam twist: FK cascade behaviour What happens on delete of a referenced row? Ex 10

The figure below is the map of how the key-types nest — keep it in your eye while reading; every example lands somewhere inside these rings.

Figure — Keys — primary, candidate, super, foreign, natural vs surrogate

Worked examples

Example 1 — the simplest possible key (cell C1)


Example 2 — the composite key (cell C2)


Example 3 — super keys that get chopped (cell C3)


Example 4 — the degenerate duplicate (cell C4)


Example 5 — NULLs in a would-be primary key (cell C5)


Example 6 — foreign key that repeats and goes NULL (cell C6)


Example 7 — when only the whole row is unique (cell C7)


Example 8 — natural vs surrogate, real-world word problem (cell C8)


Example 9 — exam twist: counting super keys (cell C9)


Example 10 — exam twist: foreign key on delete (cell C10)


Recall Quick self-test (click to reveal)

Single guaranteed-unique column — super key, candidate key, or both? ::: Both (unique ⇒ super; single column ⇒ minimal ⇒ candidate). Table R(A,B,C,D) with sole candidate key {A} — how many super keys? ::: . Can a foreign key column contain duplicates? ::: Yes — only the referenced key must be unique. Can a column with some NULLs be a primary key? ::: No — primary keys must be NOT NULL; it can be a UNIQUE alternate key instead. ISBN changes on reprint and is sometimes missing — good primary key? ::: No; use a surrogate id as PK and keep isbn UNIQUE. If every column and every pair repeats, what is the candidate key? ::: The whole row (all columns together) — an all-key relation. What does ON DELETE CASCADE do to child rows? ::: Deletes them along with the referenced parent row.


Connections

  • Parent topic
  • Functional Dependencies — why {student_id, course_id} → grade names the candidate key
  • Referential Integrity — the delete/cascade behaviour in Ex 10
  • Relational Model — rows-as-a-set, the reason keys exist at all
  • Normalization — candidate keys drive the normal forms
  • Indexes — primary and unique keys are backed by indexes