Overview
Curated: · Written: · Reviewed:
Schema Design & Normalization
In one line
Normalize until every fact is stored once, so an update cannot leave the database disagreeing with itself. Break that on purpose only when a measured read pattern justifies copying a value, and name what the copy costs to keep consistent.
Key definitions
- Functional dependency
X → Y: rows that agree on X must agree on Y. - 1NF: each column holds one atomic value. A
tagscolumn you filter by a single tag violates it. - 2NF: every non-key column depends on the whole primary key. Depending on part of a composite key is a partial dependency.
- 3NF: no non-key column depends on another non-key column (a transitive dependency).
- Surrogate key with unique natural key: the surrogate is the stable reference, and the unique constraint keeps the real-world rule enforced.
- Snapshot vs copy: a point-in-time fact such as price paid belongs on the child row. A copy of a current value is redundancy you must keep in sync.
How it works
Constraints live in the database, so they hold for every writer. Rules in application code hold only while every writer remembers them.
A check-then-insert is a race. Two transactions both see no row and both insert. A unique index, partial unique index or exclusion constraint makes the check and the insert atomic.
A denormalized copy needs three things named: the read/write ratio, the write amplification, and the mechanism that keeps copies honest (same-transaction trigger, generated column, or reconciliation job).
Schema changes follow expand, migrate, contract. Add a NOT VALID constraint, then VALIDATE it separately to avoid a long lock.
When to use and when not to
| Choice | Use when | Avoid when |
|---|---|---|
| Normalized tables | Values change and writes are frequent | A join on the hot path is measured as the bottleneck |
| Denormalized copy | Read/write ratio is high and the value rarely changes | The source changes often or is written from many paths |
| Snapshot column | The fact is point-in-time, such as price at purchase | The value should track the current source |
| Surrogate key plus natural unique | Business identifiers may change | The identifier is stable and never referenced elsewhere |
| JSON column | Attributes vary per row or the payload is opaque | Keys are fixed and get filtered, joined or aggregated |
| Lookup table | The set changes or rows need attributes | A small fixed set with no attributes (use a CHECK) |
| Partial unique index | One active row per entity while keeping history | The database lacks partial indexes (use a generated column) |
Traps interviewers probe
- Enforcing uniqueness with SELECT then INSERT. Fix: declare a unique, partial unique or exclusion constraint so the database enforces it under concurrency.
- Dropping the unique constraint because the surrogate is the primary key. Fix: keep both. The surrogate gives stable references, and the unique constraint keeps duplicates out.
- Treating a copied current value as a snapshot. Fix: ask whether it is the same fact or a point-in-time record. Copy only the latter onto the child row.
- Overwriting in place when history will be asked for. Fix: model validity ranges or an append-only event table before the column exists. The past cannot be recovered later.
- Adding a constraint or NOT NULL in one step on a live table. Fix: add
NOT VALID, validate separately, and run expand, migrate, contract across separate deploys.
Interview tips
Open with the access patterns: which queries run, the cardinality of the columns they filter on, and the read/write ratio. Then draw the normalized shape and say which part you would denormalize and why.
Expect these follow-ups: what happens under concurrent writes; which index you would add and why that column order; how the design changes at ten times the data; how you would backfill existing rows; and what changes if the read/write ratio is inverted.
30-second outline: Start from the queries and the read/write ratio. Normalize to 3NF and name any functional dependency that violates it. Enforce uniqueness and overlap rules in the database, not in a validator. Denormalize only the one value the hot path needs, and name the trigger or reconciliation that keeps it correct. Finish with the expand, migrate, contract sequence for rolling it out.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.