Kayavak / Databases / DB·55 The Relational Model / Deeper
Deeper · Databases
The Relational Model
The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.
Why does this exist?
Edgar Codd was a mathematician, and his 1970 paper brought set theory and logic to data: a table is a relation, a set of tuples; queries are operations on relations (selection, projection, join) whose results are relations too. That closure is why queries compose. In the following years Codd and others described normal forms, rules for splitting data into tables so that each fact is stored once, because duplicated facts get updated in one place and not another. Boyce–Codd normal form dates from 1974.
Real SQL departed from the model in ways critics such as Chris Date have argued about since: tables can contain
duplicate rows, column order is visible, and NULL brings three-valued logic, where a comparison can be true, false or
unknown. Those departures are behind several of the chapter's pitfalls.
The mental model
Try it yourself first: write your own model in the Mental Model Explorer, then compare.
Store each fact once, in the table whose key it depends on; reconstruct everything else with joins. Keys identify, constraints protect, and normalisation prevents one fact from having two versions.
| Question | Answer |
|---|---|
| What is it? | Data as relations with typed attributes, keys, constraints and relationships expressed by matching values |
| Problem it solves | Representing information without redundancy, so updates can't contradict each other |
| Parts | Relations, attributes, candidate and primary keys, foreign keys, functional dependencies, normal forms, NULL |
| How it works | Design tables so every non-key attribute depends on the key; join on keys to combine facts |
| Assumptions | Functional dependencies are known and stable; NULL's meaning is clear |
| When they change | Unnormalised data needs every copy updated together; NULL makes NOT IN and comparisons return surprising results |
| Where it fails | Very read-heavy workloads may denormalise deliberately, for speed, at the cost of keeping copies in sync |
| Related ideas | Sets, relations and logic (Foundations, ch05); SQL (ch56); NoSQL's access-pattern design (ch62) |
What if?
What if a customer's address were stored on every order?
Reason it through, then open
Changing the address means updating every order, and missing one leaves the customer with two addresses (an update anomaly); a customer with no orders has nowhere to store an address at all (an insertion anomaly). Storing the address once, keyed by customer, removes both. If you need the address as it was when the order shipped, that's a different fact (the shipping address), and storing it on the order is correct.
What if NULL meant false?
Reason it through, then open
SQL would be two-valued and simpler, but "unknown" would be indistinguishable from "known to be no". Real SQL's
three-valued logic is why WHERE x NOT IN (SELECT y …) returns no rows when any y is NULL: for each x the database
can't be sure it's not equal to the unknown value. NOT EXISTS avoids the trap.
Where the boundary is
| Aspect | Natural key | Surrogate key |
|---|---|---|
| Is | A real-world identifier (email, ISBN) | A generated id with no meaning |
| Can change | Yes: people change emails | Never |
| Exposes information | Often | No |
| Use | As a UNIQUE constraint | As the primary key, usually |
Derive it
When splitting a table loses nothing (Heath's theorem).
- Take a table R with attributes X, Y and Z, and split it into R1(X, Y) and R2(X, Z). Joining them back on X gives every original row, and possibly extra rows that were never there.
- Suppose X → Y: each X value determines exactly one Y.
- Joining R1 and R2 on a given x pairs every (x, y) row with every (x, z) row. But there's only one y for that x, so each (x, z) gets exactly the y it originally had.
- So the join produces exactly the original rows: the decomposition is lossless.
- Without the dependency, an x with two y values and two z values would join into four rows where there were two: spurious data. That's why normalisation splits tables along functional dependencies, and only along them.
How sure are we?
| Claim | How it's known | Where from |
|---|---|---|
| A split along a functional dependency is lossless | Theorem | Heath, 1971 |
| Normalisation prevents update anomalies | Theorem, under its definitions | Normal-form theory from Codd onwards |
NOT IN with a NULL in the subquery returns nothing |
Convention | SQL's three-valued logic in the standard |
| Denormalise for read speed | Model | Sometimes right; costs synchronisation and correctness risks |
Still open
Whether SQL's NULL design was a mistake is a long-running argument; alternatives (separate tables for optional facts, multiple kinds of missing value) have advocates and no consensus.
What would change your mind? If a workload had data that never changed after writing, update anomalies couldn't happen, and denormalising for reads would cost little.
Recall
What does Heath's theorem say?
Splitting R(X, Y, Z) into (X, Y) and (X, Z) is lossless if X determines Y.
What's an update anomaly?
A fact stored in several places, so updating one copy leaves the data contradicting itself.
Why can `NOT IN` return no rows?
If the subquery contains NULL, every comparison is unknown, so no row qualifies.
Why prefer surrogate primary keys?
They never change and carry no meaning; natural keys can change.
Reflect
Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.