{}DSA Atlas

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.

Beginner → Intermediate7 interview questions

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

KeyDefinitionExample in user(id, email, phone, name)
Super keyany attribute set that's unique{id}, {email}, {id, name}
Candidate keyminimal super key{id}, {email}, {phone}
Primary keythe chosen candidate keyid
Alternate keyother candidate keysemail, phone (UNIQUE)
Foreign keyreferences a key elsewhereorders.user_id → user.id
Surrogate keyartificial ididentity/UUID id
Natural keyreal-world identifieremail, 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 DELETE behaviour — 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} → grade
  • zip → city

Armstrong's axioms

  1. Reflexivity: if Y ⊆ X then X → Y.
  2. Augmentation: if X → Y then XZ → YZ.
  3. 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_idcustomer_idcustomer_namecityproduct_idproduct_nameunit_priceqty
110AshaPune501Mouse4992
110AshaPune502Keyboard12991
211RaviDelhi501Mouse4991
  • 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:

Text
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:

Text
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

studentsubjectteacher
S1MathMr. A
S2MathMr. A
S1PhysicsMs. B
S3MathMr. 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

FormRuleRemoves
1NFatomic values, unique rowsrepeating groups
2NFno partial dependency on a composite keyredundancy from part-key facts
3NFno transitive dependency (non-key → non-key)redundancy from facts about non-keys
BCNFevery determinant is a super keyremaining FD anomalies
4NFno non-trivial multivalued dependenciesindependent multi-valued facts
5NFno non-trivial join dependenciescyclic decomposition anomalies

Decomposition properties

  • Lossless join (must have): R1 ∩ R2 is 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:

TechniqueExampleKeep in sync via
Duplicated columnposts.author_nametransaction / trigger / event consumer
Counter cacheposts.comment_countatomic increment, periodic reconciliation
Materialised viewdaily revenue per regionREFRESH MATERIALIZED VIEW, incremental views
Pre-joined read modelproduct page document in ElasticsearchCDC / outbox events (CQRS)
Star schemafact_sales + wide dim_productETL/ELT jobs

Interview questions

0 / 7 marked confident

Answer out loud first, then open the model answer. Mark a question confident only when you could explain it to an interviewer without notes.