Relational Model, Keys & Normalization
Relations, keys (super, candidate, primary, alternate, foreign, surrogate), integrity constraints, functional dependencies and closures, anomalies, 1NF through BCNF with one running example, 4NF/5NF, lossless and dependency-preserving decomposition, and when to denormalize.
The relational model in one minute
- A relation is a set of tuples (rows) with the same attributes (columns) — a table.
- A domain is the set of allowed values for an attribute.
- Relations are sets: no duplicate rows, no inherent order (SQL tables relax this — they're multisets unless you add a key).
- Operations come from relational algebra: selection σ (
WHERE), projection π (SELECT cols), join ⋈, union ∪, difference −, rename ρ, plus aggregation and grouping.
Keys
| Key | Definition | Example in user(id, email, phone, name) |
|---|---|---|
| Super key | any attribute set that's unique | {id}, {email}, {id, name} |
| Candidate key | minimal super key | {id}, {email}, {phone} |
| Primary key | the chosen candidate key | id |
| Alternate key | other candidate keys | email, phone (UNIQUE) |
| Foreign key | references a key elsewhere | orders.user_id → user.id |
| Surrogate key | artificial id | identity/UUID id |
| Natural key | real-world identifier | email, ISBN, VIN |
Integrity constraints
- Domain: type,
CHECK (price >= 0), enums. - Entity integrity: primary key is unique and
NOT NULL. - Referential integrity: foreign keys point to existing rows; choose
ON DELETEbehaviour —RESTRICT/NO ACTION(default),CASCADE,SET NULL,SET DEFAULT. - Key constraints:
UNIQUE. - Business rules:
CHECK, exclusion constraints (Postgres: no overlapping bookings), triggers.
Functional dependencies (FDs)
X → Y ("X determines Y") means: rows that agree on X always agree on Y.
student_id → student_name{student_id, course_id} → gradezip → city
Armstrong's axioms
- Reflexivity: if Y ⊆ X then X → Y.
- Augmentation: if X → Y then XZ → YZ.
- Transitivity: if X → Y and Y → Z then X → Z.
Derived: union (X → Y, X → Z ⇒ X → YZ), decomposition, pseudotransitivity.
Attribute closure X⁺
Start with X; repeatedly add the right side of any FD whose left side is already included. If X⁺ contains all attributes, X is a super key.
Example: R(A, B, C, D, E), FDs: A → B, BC → D, D → E. {A, C}⁺ = {A, C} → +B (A → B) → +D (BC → D) → +E (D → E) = all → {A, C} is the candidate key.
Anomalies: why normalize
One wide table:
| order_id | customer_id | customer_name | city | product_id | product_name | unit_price | qty |
|---|---|---|---|---|---|---|---|
| 1 | 10 | Asha | Pune | 501 | Mouse | 499 | 2 |
| 1 | 10 | Asha | Pune | 502 | Keyboard | 1299 | 1 |
| 2 | 11 | Ravi | Delhi | 501 | Mouse | 499 | 1 |
- Update anomaly: Asha moves → update every row or end up with two cities.
- Insert anomaly: can't add a product nobody has ordered yet.
- Delete anomaly: delete order 2 → Ravi disappears entirely.
The normal forms, step by step
1NF — atomic values, no repeating groups
✗ customer(id, name, phones = "98..., 99...")
✓ customer(id, name) + customer_phone(customer_id, phone)
2NF — no partial dependencies
Applies when the key is composite. In the table above, the key is (order_id, product_id), but:
order_id → customer_id, customer_name, city(depends on part of the key)product_id → product_name, unit_price(depends on part of the key)
Split:
orders(order_id, customer_id, customer_name, city)
products(product_id, product_name, unit_price)
order_items(order_id, product_id, qty)
3NF — no transitive dependencies
In orders(order_id, customer_id, customer_name, city): order_id → customer_id → customer_name, city. Non-key attributes depend on another non-key attribute. Split:
customers(customer_id, customer_name, city)
orders(order_id, customer_id)
Formal 3NF test: for every FD X → A, either X is a super key or A is a prime attribute (part of a candidate key).
BCNF — every determinant is a super key
| student | subject | teacher |
|---|---|---|
| S1 | Math | Mr. A |
| S2 | Math | Mr. A |
| S1 | Physics | Ms. B |
| S3 | Math | Mr. C |
FDs: {student, subject} → teacher, teacher → subject. Candidate keys: {student, subject}, {student, teacher}. It's 3NF (subject is prime) but teacher → subject violates BCNF (teacher isn't a super key). Anomaly: if Mr. A switches subjects you must update many rows.
Decompose: teacher_subject(teacher, subject) + student_teacher(student, teacher) → BCNF, lossless, but the FD {student, subject} → teacher can no longer be enforced by a single table's key.
4NF — no multivalued dependencies
employee_skill_language(emp, skill, language) where skills and languages are independent: every combination must be stored (emp has 3 skills × 2 languages = 6 rows). MVDs emp ↠ skill, emp ↠ language. Split into emp_skill(emp, skill) and emp_language(emp, language).
5NF — no join dependencies
A table that can only be losslessly reconstructed by joining three or more projections (supplier–part–project cyclic rules). Rare in practice.
Summary
| Form | Rule | Removes |
|---|---|---|
| 1NF | atomic values, unique rows | repeating groups |
| 2NF | no partial dependency on a composite key | redundancy from part-key facts |
| 3NF | no transitive dependency (non-key → non-key) | redundancy from facts about non-keys |
| BCNF | every determinant is a super key | remaining FD anomalies |
| 4NF | no non-trivial multivalued dependencies | independent multi-valued facts |
| 5NF | no non-trivial join dependencies | cyclic decomposition anomalies |
Decomposition properties
- Lossless join (must have):
R1 ∩ R2is a key of R1 or R2. - Dependency preservation (nice to have): every FD checkable within one table.
- 3NF can always be achieved with both; BCNF always lossless but sometimes not dependency-preserving.
Denormalization: breaking the rules on purpose
Normalised schemas optimise for write correctness. Read-heavy systems often add controlled redundancy:
| Technique | Example | Keep in sync via |
|---|---|---|
| Duplicated column | posts.author_name | transaction / trigger / event consumer |
| Counter cache | posts.comment_count | atomic increment, periodic reconciliation |
| Materialised view | daily revenue per region | REFRESH MATERIALIZED VIEW, incremental views |
| Pre-joined read model | product page document in Elasticsearch | CDC / outbox events (CQRS) |
| Star schema | fact_sales + wide dim_product | ETL/ELT jobs |
Interview questions
Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.