Normalization & Schema Design
Functional dependencies, update anomalies, 1NF through BCNF with worked examples, when to denormalise, and practical schema design advice.
Normalisation organises tables so that each fact is stored exactly once. That prevents inconsistencies when data changes. Denormalisation deliberately duplicates data for speed. Good schema design knows when to use each.
The problem: anomalies
A single wide table of orders:
| order_id | customer | customer_email | product | product_price | qty |
|---|---|---|---|---|---|
| 1 | Ada | ada@x.io | Keyboard | 80 | 1 |
| 1 | Ada | ada@x.io | Mouse | 25 | 2 |
| 2 | Alan | alan@x.io | Keyboard | 80 | 1 |
| Anomaly | Example |
|---|---|
| Update | Ada changes her email: you must update every one of her rows, and missing one leaves inconsistent data |
| Insert | You can't add a new product until someone orders it |
| Delete | Deleting order 2 also deletes the only record that Alan exists |
The cause: the table mixes facts about orders, customers and products.
Functional dependencies
X → Y ("X determines Y") means: rows with the same X always have the same Y.
customer → customer_emailproduct → product_price(order_id, product) → qty
Normal forms are rules about which dependencies a table may contain.
The normal forms
- 1NF — every column holds a single atomic value; no repeating groups or lists (no
phones = "555-1, 555-2", nophone1, phone2, phone3columns). - 2NF — 1NF, and no non-key column depends on only part of a composite key. (
product_pricedepends only onproduct, not on(order_id, product)→ move it out.) - 3NF — 2NF, and no non-key column depends on another non-key column (no transitive dependencies). (
customer_emaildepends oncustomer, not on the order → move it out.) - BCNF — for every dependency
X → Y, X must be a super key. A slightly stricter 3NF that handles rare edge cases with overlapping candidate keys.
The normalised design
Each fact now lives in one place. Note order_items.unit_price: it looks like duplication of products.price, but it's a different fact — the price at the time of purchase — which must not change when the catalogue price does. Recognising which "duplicates" are really separate facts is the art of modelling.
Higher normal forms
4NF removes independent multi-valued facts stored together (a person's skills and languages in one table create a cross product). 5NF deals with join dependencies. They rarely come up in practice; 3NF/BCNF is the usual target.
Denormalisation
Normalised schemas need joins, and joins at very large scale or in hot paths can be expensive. Denormalisation stores redundant data to speed up reads:
| Technique | Example | Cost |
|---|---|---|
| Duplicated columns | Store customer_name on orders for listing pages | Must update both on change |
| Precomputed aggregates | posts.comment_count | Keep it in sync (triggers, the app, or async jobs) |
| Materialised views | Dashboard tables refreshed periodically | Staleness |
| Embedded documents | MongoDB order document containing its items | Harder to query items across orders |
| Star schema | Analytical fact table + dimension tables | Designed for reads, loaded by ETL |
OLTP vs OLAP schemas
| OLTP (transactional) | OLAP (analytical) | |
|---|---|---|
| Workload | Many small reads and writes | Few huge scans and aggregations |
| Schema | Normalised (3NF) | Denormalised star / snowflake schemas |
| Storage | Row-oriented | Column-oriented |
| Examples | PostgreSQL, MySQL | Snowflake, BigQuery, ClickHouse, DuckDB |
Practical schema design checklist
- Pick clear, consistent names (
snake_case, singular or plural, but consistent). - Give every table a primary key; add UNIQUE constraints on natural keys.
- Declare foreign keys and index the FK columns.
- Use the right types:
TIMESTAMPTZfor instants,NUMERICfor money,BOOLEAN, enums or lookup tables for fixed sets. - Prefer NOT NULL; allow NULL only when "unknown" is genuinely meaningful.
- Add
created_at/updated_at; consider soft deletes (deleted_at) when you need history. - Model the domain, not the screens — UIs change faster than the data.
- Normalisation stores each fact once to avoid update, insert and delete anomalies.
- 1NF: atomic values. 2NF: no partial dependencies. 3NF: no transitive dependencies. BCNF: every determinant is a key.
- "The key, the whole key, and nothing but the key."
- Denormalise deliberately for read performance, with a plan for consistency.
- OLTP favours normalised row stores; analytics favours star schemas in column stores.
QWhen would you denormalise?medium
When a hot read path needs data from many joins and measurements show the joins are the bottleneck; when you need precomputed aggregates (counters, totals); or in analytics and read-optimised stores. Always define how the copies stay consistent: transactions, triggers, or asynchronous events with acceptable staleness.