Normalization is usually taught with definitions about functional dependencies and candidate keys. Those definitions are precise, but they rarely help when you are staring at a table someone designed in a hurry three years ago. This post goes the other way: one realistic table, the problems it causes, and each normal form as the fix for a specific problem.
Then the second half, which textbooks tend to skip: when to copy data on purpose, and how to do it without creating the same problems again.
The starting table
An early version of an online shop stored orders like this:
order_id | order_date | customer_email | customer_name | customer_city | items
---------+------------+---------------------+---------------+---------------+------------------------------------------
1001 | 2026-03-02 | ana@example.com | Ana Lopes | Porto | SKU-1:Blue Mug:2:12.00, SKU-7:Tea Box:1:18.50
1002 | 2026-03-02 | ben@example.com | Ben Okafor | Leeds | SKU-7:Tea Box:3:18.50
1003 | 2026-03-04 | ana@example.com | Ana Lopes | Lisbon | SKU-1:Blue Mug:1:12.00
It works for printing an order. It fails at almost everything else.
First normal form: one value per cell
Problem. items holds a list packed into a string. To answer "how many Tea Boxes did we sell in
March?" you have to parse text in every row. You cannot index it, constrain it, or join on it. Changing one
item's quantity means rewriting the whole string.
Rule. Each column holds a single value, and each row is identified by a key. No lists, no repeating
groups like item1, item2, item3 columns.
Fix. One row per order line:
order_id | line | order_date | customer_email | customer_name | customer_city | sku | product_name | qty | unit_price
---------+------+------------+-----------------+---------------+---------------+-------+--------------+-----+-----------
1001 | 1 | 2026-03-02 | ana@example.com | Ana Lopes | Porto | SKU-1 | Blue Mug | 2 | 12.00
1001 | 2 | 2026-03-02 | ana@example.com | Ana Lopes | Porto | SKU-7 | Tea Box | 1 | 18.50
1002 | 1 | 2026-03-02 | ben@example.com | Ben Okafor | Leeds | SKU-7 | Tea Box | 3 | 18.50
1003 | 1 | 2026-03-04 | ana@example.com | Ana Lopes | Lisbon | SKU-1 | Blue Mug | 1 | 12.00
The key is (order_id, line). Now every question about items is a simple WHERE and GROUP BY. But
look how much repeats.
Second normal form: every column depends on the whole key
Problem. order_date and the customer columns depend only on order_id, not on (order_id, line).
They repeat on every line of the same order. That causes three anomalies, the classic trio:
- Update anomaly: fix a typo in order 1001's date and you must update every line, or the order ends up with two dates.
- Insert anomaly: you cannot record an order header with no lines yet (a draft order) without inventing a fake line.
- Delete anomaly: delete the last line of an order and the order's date and customer disappear with it.
Rule. In a table with a composite key, no column may depend on only part of the key.
Fix. Split the order header from the lines:
CREATE TABLE orders (
order_id bigint PRIMARY KEY,
order_date date NOT NULL,
customer_email text NOT NULL,
customer_name text NOT NULL,
customer_city text NOT NULL
);
CREATE TABLE order_lines (
order_id bigint REFERENCES orders,
line int,
sku text NOT NULL,
product_name text NOT NULL,
qty int NOT NULL CHECK (qty > 0),
unit_price numeric(10,2) NOT NULL,
PRIMARY KEY (order_id, line)
);
product_name in order_lines also violates 2NF in spirit: it depends on sku, not on the line. We will
come back to it, because it turns out to be one of the deliberate copies.
Third normal form: no column depends on another non-key column
Problem. In orders, customer_name and customer_city depend on customer_email, which is not the
key of orders. Ana's name appears on every order she places. When she moves from Porto to Lisbon, which
row is right? In the sample data, order 1001 says Porto and 1003 says Lisbon. Is that a history of her
address, or a data entry inconsistency? The table cannot tell you.
Rule. Every non-key column depends on the key, the whole key, and nothing but the key. No transitive dependencies through another column.
Fix. Customers and products become their own tables:
CREATE TABLE customers (
customer_id bigint PRIMARY KEY,
email text NOT NULL UNIQUE,
name text NOT NULL,
city text NOT NULL
);
CREATE TABLE products (
sku text PRIMARY KEY,
name text NOT NULL,
price numeric(10,2) NOT NULL
);
ALTER TABLE orders
DROP COLUMN customer_email, DROP COLUMN customer_name, DROP COLUMN customer_city,
ADD COLUMN customer_id bigint NOT NULL REFERENCES customers;
Now Ana's name and city live in exactly one row. Change them once, and every query sees the change.
There are higher normal forms (BCNF, 4NF, 5NF). They matter for tables with overlapping candidate keys or independent multi-valued facts. In years of schema reviews, I have seen 3NF be the level where nearly all practical problems are solved, and BCNF violations are rare enough to handle case by case.
Where 3NF gives the wrong answer
Here is the catch. With a fully normalized schema, reprinting Ana's order 1001 from March would show her current city, Lisbon, and the product's current name and price. If the Blue Mug is now 14.00, the reprinted invoice is wrong, and invoices are legal documents.
This is not a reason to abandon normalization. It is a sign that some data is not what it seems. The shipping address on an order is not "the customer's city"; it is "where this order was shipped, as of the moment it was placed". The price on an order line is not "the product's price"; it is "the price the customer agreed to pay". Those are different facts, and they belong on the order.
ALTER TABLE orders ADD COLUMN ship_city text NOT NULL; -- snapshot at order time
-- order_lines.unit_price and product_name stay: agreed price and name, never updated
COMMENT ON COLUMN order_lines.unit_price IS 'Price agreed at order time; not synced with products.price';
Strictly, this is not denormalization at all: each column depends on the order, the key. The trap is thinking of it as a copy of the customer's city and then "helpfully" updating it when the customer moves.
Deliberate denormalization
Some copies really are copies, made for speed. Examples from our own systems:
| Copy | Why | Source of truth |
|---|---|---|
orders.total_amount |
sum of lines, shown on every order list | order_lines |
products.review_count, avg_rating |
product grid sorts by rating, 40M reviews | reviews |
customers.last_order_at |
segmentation queries on 2M customers | orders |
| search index document per product | full-text and facet search | products + related tables |
Each one avoids an expensive join or aggregate on a hot path. Each one can also drift out of sync with its source, which is the update anomaly that normalization was meant to prevent, now brought back on purpose.
The rule that makes copies safe
Every copied value has one owner and one refresh path, written down.
- Owner: the code that is allowed to write the copy. For
review_count, only the review service's "review created/deleted" handler. No admin screen, no batch script, no other service updates it directly. - Refresh path: how the copy is kept in sync, and how fast. In the same transaction as the source write (strongest), by a trigger, by an event consumer (seconds), or by a scheduled rebuild (minutes to hours).
- Repair job: a periodic job that recomputes the copy from the source and reports differences. If the report is ever non-empty, the refresh path has a bug.
For orders.total_amount we update it in the same transaction as the lines:
BEGIN;
INSERT INTO order_lines (order_id, line, sku, qty, unit_price) VALUES (1001, 3, 'SKU-9', 1, 7.00);
UPDATE orders
SET total_amount = (SELECT sum(qty * unit_price) FROM order_lines WHERE order_id = 1001)
WHERE order_id = 1001;
COMMIT;
And a nightly check:
SELECT o.order_id, o.total_amount, sum(l.qty * l.unit_price) AS computed
FROM orders o JOIN order_lines l USING (order_id)
WHERE o.updated_at >= now() - interval '2 days'
GROUP BY o.order_id, o.total_amount
HAVING o.total_amount <> sum(l.qty * l.unit_price);
For review_count an event consumer updates it within seconds. Small temporary differences are fine
there; nobody is harmed if a product shows 211 reviews for a few seconds instead of 212.
Before you denormalize
Copies are a cost you pay forever. Things we try first:
- An index. Many "slow joins" are missing indexes on the foreign key.
- A covering index or a better query. Check
EXPLAINbefore restructuring. - A materialized view (PostgreSQL), refreshed on a schedule, when slightly stale data is acceptable. It keeps the copy logic in the database and out of application code.
- A cache in front of the query, for read-heavy data with clear invalidation.
If none of those is enough, copy the data, and write down the owner, the refresh path and the repair job in the same pull request.
Summary
- 1NF: one value per cell. 2NF: no dependency on part of a composite key. 3NF: no dependency through another non-key column. Each step removes a specific kind of anomaly.
- Facts captured at a moment in time, like an agreed price or a shipping address, belong on the record they describe. They are not copies.
- Denormalize for real performance needs, after indexes and query fixes.
- Every copied value needs one owner, one refresh path and a job that checks it against the source.
References: PostgreSQL constraints, PostgreSQL materialized views, MySQL foreign key constraints.
Comments
0No comments yet. Be the first to comment!
Sign In to post a comment.