4.4.2 · D4Databases

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

2,172 words10 min readBack to topic

Throughout, we reuse one running table. Read it carefully — every value here is a real row, and "unique" always means unique across the actual data plus any future data the rule promises.

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

The picture above is the whole exercise set in one diagram: which columns pin down a single row, and which repeat. Keep glancing back at it.


Level 1 — Recognition

Recall Solution 1.1

What we do: apply two questions in order — Q1: is it unique? (super key) Q2: can we remove a column and stay unique? (if no, it's also candidate/minimal).

  • (a) — unique by rule → super key. Single attribute, nothing to remove → also candidate key. ✅
  • (b) — two "John Smith"s allowed → not unique → neither. ✅
  • (c) — contains emp_id which is already unique, so the pair is unique → super key. But drop name and is still unique → not minimal → not a candidate key. So: super key only. ✅
  • (d) — unique by rule, single attribute → super key AND candidate key. ✅
Recall Solution 1.2

False. A foreign key column repeats freely — one student enrols in many courses, so roll_no appears many times. Only the referenced key (Student.roll_no) is unique. This is confusion (c) from the parent note.


Level 2 — Application

Recall Solution 2.1

No. A primary key must be unique AND NOT NULL. phone is unique but may be NULL, and a NULL means "unknown" — it cannot identify a row. So phone fails the NOT-NULL half of the rule. It could be enforced with a UNIQUE constraint (which allows NULLs), but never as PRIMARY KEY.

Recall Solution 2.2
CREATE TABLE Employee (
  emp_id  INT PRIMARY KEY,          -- chosen candidate key
  email   VARCHAR(100) UNIQUE,      -- alternate key
  phone   VARCHAR(20)  UNIQUE,      -- unique, NULL allowed
  dept_id INT,
  name    VARCHAR(50),
  FOREIGN KEY (dept_id) REFERENCES Department(dept_id)
);

Why: PRIMARY KEY auto-enforces unique + not null (the official handle). UNIQUE on email keeps it an honest alternate key. The FOREIGN KEY clause is the glue enforcing Referential Integrity — you cannot insert a dept_id that has no matching department row.


Level 3 — Analysis

Recall Solution 3.1

What determines a row? The rule "no room double-booked on a date" means the pair is unique. So is a super key.

  • Remove date: — one room appears on many dates → not unique → not a super key.
  • Remove room: — one date has many rooms → not unique → not a super key.

Neither single column works, so is minimal → it is a candidate key. Since nothing else is unique, it is the only candidate key. {room} alone fails because a room recurs across dates — it does not functionally determine the whole row.

Recall Solution 3.2

is unique (it contains email, already unique) → super key. But remove phone: is still unique → the subset works → the pair is not minimalnot a candidate key. Only super key.


Level 4 — Synthesis

Recall Solution 4.1

Three tables; the many-to-many between student and course needs a junction table.

CREATE TABLE Student (
  roll_no INT PRIMARY KEY,
  name    VARCHAR(50)
);
CREATE TABLE Course (
  course_id  INT PRIMARY KEY,
  title      VARCHAR(50),
  teacher    VARCHAR(50)          -- one teacher per course
);
CREATE TABLE Takes (
  roll_no   INT,
  course_id INT,
  grade     CHAR(2),
  PRIMARY KEY (roll_no, course_id),          -- composite key
  FOREIGN KEY (roll_no)   REFERENCES Student(roll_no),
  FOREIGN KEY (course_id) REFERENCES Course(course_id)
);

Why: Takes has a composite primary key — a student can take a course only once, and this pair is exactly what makes a row unique. Both parts are also foreign keys pointing back to the parent tables — this is normal and correct. grade is a plain attribute depending on the whole key (satisfying 2NF, see Normalization).

Recall Solution 4.2

Failure: an employee changes their email. Because email was the primary key, every foreign key row referencing them (payroll, enrolments, logs) must be updated in lock-step, or referential integrity breaks. Emails are also long strings → fat indexes.

Fix: use a surrogate emp_id (auto-increment) as primary key — stable and meaningless, so it never changes — and keep email VARCHAR(100) UNIQUE as an alternate key to still block duplicate people. This is confusion (d) resolved: surrogate for the handle, natural key kept under a UNIQUE constraint.


Level 5 — Mastery

Recall Solution 5.1

What makes a set a super key here? A set is a super key iff it contains or contains (either alone determines the whole row; any superset of a super key is a super key).

Total attribute-subsets of : . Count the ones that are not super keys — those contain neither nor , i.e. subsets of : there are of them ().

Recall Solution 5.2

Fix . The remaining attributes are each free (in or out): subsets that contain . From these subtract the ones with neither nor : those are subsets of that contain , namely and of them.

Recall Solution 5.3

The employee row survives; its dept_id becomes NULL. Integrity still holds: a foreign key column is allowed to be NULL, and NULL means "references nothing" — which is a legal state, not a dangling pointer. It would only break if dept_id still held 7 after department 7 vanished. (Contrast: ON DELETE RESTRICT would have blocked the department deletion; CASCADE would have deleted the employee row too.)


Recall Feynman recap (click to reveal)
  • Super key = any name-tag description that's unique — extra words allowed.
  • Candidate key = the shortest such descriptions (nothing removable).
  • Primary key = the one candidate key we officially stamp on the tag — must never be blank.
  • Foreign key = borrowing another table's tags; the borrowed number can repeat and can be blank.
  • Natural vs surrogate = a true fact (email) vs a stable meaningless ticket (id).

Connections

  • Relational Model — the table-as-set foundation these exercises assume
  • Functional Dependencies — the FDs behind Exercises 3.1 and 5.x
  • Referential Integrity — the foreign-key delete rules in 5.3
  • Normalization — the composite key in 4.1 satisfies 2NF
  • Indexes — why fat natural keys (4.2) hurt