Topic 2 of 5 · 5 min read
Normalizing and deliberate denormalization
Goal: Spot the update anomaly in a denormalized table and decide whether to split it or keep the copy with a stated sync rule.
Store each fact once, and copy it on purpose only when you can say how the copy stays correct. Normalization is the habit of giving every fact exactly one home.
Check yourself · 1 of 3
A table stores customer_email on every order row. What problem does this create?
Why it matters
When the same fact lives in several rows, someone will update some of them and miss the rest. These bugs are called update anomalies, and they produce reports where one customer has two addresses and nobody knows which is current. AI assistants often produce one wide table because it is the shortest answer to "store my orders".
How to do it
- Find repeated facts. In an
orderstable withcustomer_nameandcustomer_emailon every row, the customer's details repeat once per order. - Name the anomaly. Update: change an email and you must touch 40 rows. Insert: you cannot record a customer before their first order. Delete: removing the last order erases the customer.
- Apply the plain-language normal forms.
- First: one value per cell, no lists in a column.
- Second: every column depends on the whole key, not part of it.
- Third: every column depends on the key and nothing but the key. A
citythat follows frompostal_codebelongs in its own table.
- Split, then join. Put customers in their own table and reference them by id.
- Denormalize only with a reason and a rule. Reading is slow, you measured it, and an index did not fix it. Then copy the value, and write down who keeps it in sync: a trigger, a transaction in the write path, or a nightly job.
Before and after
| order_id | customer_id | customer_email | total |
|---|---|---|---|
| 101 | 7 | ana@example.com | 40.00 |
| 102 | 7 | ana.old@example.com | 12.50 |
Customer 7 has two emails. After normalizing, orders keeps only customer_id, and customers holds the email once.
A legitimate copy: orders.shipping_address snapshotted at purchase time. The customer may move later, but the order must still show where it was sent. Here the copy is the correct model, because the fact is "address at purchase".
What goes wrong
- Copies with no owner. A cached
order_counton the customer row drifts the first time a code path forgets to update it. - Normalizing everything. Splitting a value that never changes into five joined tables adds cost and no safety.
- Denormalizing before measuring. A missing index is a cheaper fix than a second copy of the data.
- Mixing snapshot and live data. Showing a current price on an old invoice rewrites history.
Examples
The two-email customer
An orders table repeats customer_email on every row. Support changes it on the newest order only. Reports now show two emails for one customer. Fix: move the email to customers and keep customer_id on orders.
A copy with a rule
A dashboard counts a customer's orders on every page view and is slow. The team adds customers.order_count, updated inside the same transaction that inserts an order, and a nightly job recounts and logs any mismatch. The copy is deliberate and checked.
Key points
- Give every fact one home, and the same fact repeated across rows will eventually disagree.
- Update, insert and delete anomalies are the symptoms that tell you a table needs splitting.
- Third normal form in plain terms: each column depends on the key and nothing but the key.
- Denormalize only after measuring a slow read, and name the mechanism that keeps the copy in sync.
- A snapshot such as the address at purchase time is a different fact, not a bad copy.
Common mistakes
- Repeating the same customer or product details on every row of a transaction table.
- Adding a cached count or total with no code that keeps it up to date.
- Splitting data into many tables for purity when the values never change independently.
- Using a live price in historical orders instead of the price at purchase.
Check yourself · 2 of 3
Which situation justifies copying a value into another table?
Check yourself · 3 of 3
An invoice table stores the product price at the time of sale instead of reading products.price. Why is that correct?
Practice items, not counted towards your mastery.
Start here
- PostgreSQL: Data definition (opens in a new tab)DocsFree
How tables, constraints, row security and schema changes are declared in PostgreSQL.
- PostgreSQL: Constraints (opens in a new tab)DocsFree
Exact behaviour of CHECK, NOT NULL, UNIQUE, primary and foreign keys and their ON DELETE actions.
- PostgreSQL: ALTER TABLE (opens in a new tab)DocsFree
Which ALTER TABLE forms rewrite the table or take strong locks, and the NOT VALID option.
More for this topic
- SQL Antipatterns (Pragmatic Bookshelf) (opens in a new tab)BookPaid
Names and fixes for comma lists, entity-attribute-value tables and other schema traps.