Normalization

Topic 3 of 4 in Designing databases10 min readBeginnerFree course

Overview

You’ve seen that Bean & Leaf spreads its data over six tables, and that joins bring it back together. Why not keep everything in one big table, like a spreadsheet? Normalization is the answer — a set of design principles, first described by E. F. Codd, for organising tables so that each fact is stored once. It sounds academic, but its purpose is practical: a normalised database can’t contradict itself.

The trouble with one wide table

Let’s build the spreadsheet version of the shop: one row per order line, with the customer and product details copied in:

SQL
CREATE TABLE flat_sales AS
SELECT o.id AS order_id, o.ordered_at::date AS day,
       c.email AS customer_email, c.first_name || ' ' || c.last_name AS customer, c.city,
       p.name AS product, cat.name AS category, oi.quantity, oi.unit_price
FROM orders AS o
JOIN customers AS c ON c.id = o.customer_id
JOIN order_items AS oi ON oi.order_id = o.id
JOIN products AS p ON p.id = oi.product_id
LEFT JOIN categories AS cat ON cat.id = p.category_id;

SELECT order_id, customer, city, product, category, quantity
FROM flat_sales
WHERE customer_email = 'asha.rao@example.com'
ORDER BY order_id, product;
Text
SELECT 42
 order_id | customer | city |        product         | category  | quantity 
----------+----------+------+------------------------+-----------+----------
        1 | Asha Rao | Pune | Assam Breakfast        | Tea       |        1
        1 | Asha Rao | Pune | Ethiopian Yirgacheffe  | Coffee    |        2
        3 | Asha Rao | Pune | Darjeeling First Flush | Tea       |        1
        9 | Asha Rao | Pune | Electric Kettle        | Equipment |        1
        9 | Asha Rao | Pune | Jasmine Green          | Tea       |        1
       17 | Asha Rao | Pune | Ethiopian Yirgacheffe  | Coffee    |        3
       23 | Asha Rao | Pune | Assam Breakfast        | Tea       |        2
       23 | Asha Rao | Pune | Ethiopian Yirgacheffe  | Coffee    |        2
(8 rows)

Easy to read — and full of repetition. Asha’s name and city appear on eight rows; “Ethiopian Yirgacheffe is a Coffee” appears on every line that sold it. That repetition causes three kinds of problem, called anomalies.

Update anomaly. Asha moves to Mumbai. Someone updates “her” rows — but only the ones for this year:

SQL
UPDATE flat_sales
SET city = 'Mumbai'
WHERE customer_email = 'asha.rao@example.com' AND day >= '2026-06-01';

SELECT DISTINCT customer, city
FROM flat_sales
WHERE customer_email = 'asha.rao@example.com'
ORDER BY city;
Text
UPDATE 3
 customer |  city  
----------+--------
 Asha Rao | Mumbai
 Asha Rao | Pune
(2 rows)

Now the data contradicts itself: where does Asha live? Every copy of a fact is a chance for the copies to disagree.

Insert anomaly. A new customer signs up but hasn’t ordered yet. There’s no row to put them on — every row is an order line. The same goes for a new product nobody has bought.

Delete anomaly. Delete the only order line for the Decaf House Blend, and the shop forgets the product existed at all:

SQL
DELETE FROM flat_sales WHERE product = 'Decaf House Blend';
SELECT count(*) AS decaf_rows FROM flat_sales WHERE product = 'Decaf House Blend';
Text
DELETE 1
 decaf_rows 
------------
          0
(1 row)

In the real, normalised shop, none of these can happen: Asha’s city is stored once in customers, customers exist without orders, and deleting an order line leaves the product alone.

Normalization: store each fact once. Before: one wide table with columns order, customer, city, product and price, where rows repeat Asha Rao, Pune on three lines (with products Ethiopian Yirgacheffe 14.50, Pour-Over Dripper 24.00 and Darjeeling First Flush 18.00) and Ben Carter, Leeds on another. Asha’s city is stored three times: change one row and the data contradicts itself. After: one table per thing, linked by keys — customers (id, name, city), orders (id, customer_id, ordered_at), order_items (order_id, product_id, quantity, unit_price) and products (id, name, price). 1NF: one value per cell, no repeating groups. 2NF/3NF: every column describes the key, the whole key, and nothing but the key. Result: each fact lives in one place, so it can’t contradict itself. Joins bring the pieces back together when you query. Keeping unit_price on order_items is deliberate: it records the price at the time of the order, not today’s price.

The normal forms, in plain language

Database theory defines a series of normal forms. The first three cover almost everything you’ll need in practice.

First normal form (1NF): one value per cell

Each column holds a single value — no lists, no repeating groups of columns. These break 1NF:

  • a tags column containing 'gift,electric,sale';
  • columns phone1, phone2, phone3.

A list in a cell can’t be indexed, constrained or joined, and every query has to pick it apart:

SQL
SELECT name
FROM (VALUES ('Kettle', 'gift,electric'), ('Teapot', 'glass,gift'), ('Grinder', 'electric'))
     AS t(name, tags)
WHERE tags LIKE '%gift%';
Text
  name  
--------
 Kettle
 Teapot
(2 rows)

That works until someone adds a tag called giftcard. The normalised design is a separate table with one row per product–tag pair, which you can query exactly: WHERE tag = 'gift'. (PostgreSQL’s array and jsonb types are a deliberate, well-supported exception — more in Working with JSON.)

Second normal form (2NF): depend on the whole key

This applies to tables with a composite key. Every non-key column must depend on the whole key, not just part of it. If order_items (key: order_id, product_id) also had a product_name column, that name would depend only on product_id — so it would be repeated on every line that sold the product. It belongs in products.

Third normal form (3NF): nothing but the key

Every non-key column must describe the key directly, not via another column. In a customers table with city and country, the country really depends on the city (Pune is always in India), not on the customer. Strictly, that’s a 3NF violation, and a fully normalised design would have a cities table.

The classic summary: every column must depend on the key, the whole key, and nothing but the key.

How far to go

Database designers usually aim for 3NF, then make pragmatic exceptions. Bean & Leaf keeps city and country together on customers, because a separate cities table would add a join to every address query for little benefit, and addresses are often entered free-form anyway. Normalization is a guide for spotting repetition that will hurt, not a rule to follow to the last column.

Fixing a denormalised table means splitting it: move each repeated group of facts into its own table with its own key, and keep a foreign key where the data used to be. Here’s how you’d pull the products out of flat_sales:

SQL
CREATE TABLE sale_products (
    id       integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    name     text NOT NULL UNIQUE,
    category text
);

INSERT INTO sale_products (name, category)
SELECT DISTINCT product, category
FROM flat_sales
ORDER BY product;

SELECT * FROM sale_products ORDER BY id;
Text
CREATE TABLE
INSERT 0 10
 id |          name          | category  
----+------------------------+-----------
  1 | Assam Breakfast        | Tea
  2 | Burr Grinder           | Equipment
  3 | Chamomile Calm         | Tea
  4 | Colombian Supremo      | Coffee
  5 | Darjeeling First Flush | Tea
  6 | Electric Kettle        | Equipment
  7 | Ethiopian Yirgacheffe  | Coffee
  8 | Gift Card              | 
  9 | Jasmine Green          | Tea
 10 | Pour-Over Dripper      | Equipment
(10 rows)

SELECT DISTINCT collapses the repetition to one row per product, and the UNIQUE constraint guarantees it stays that way. The next step would add a product_id column to the sales table, fill it with UPDATE … FROM, and drop the copied columns — you’ll see ALTER TABLE for that in the next lesson.

Deliberate denormalization

Sometimes you want a copy, on purpose:

  • History. order_items.unit_price copies the product’s price at the time of the sale. That’s not repetition — it’s a different fact (“what this customer paid”), and it must not change when today’s price does.
  • Performance and reporting. Analytical databases and data warehouses often use wide, denormalised tables, because they’re read far more than written and joins over billions of rows are expensive.
  • Cached totals. An orders.items_total column saves summing the items every time — at the cost of keeping it in sync (with a trigger, or in the same transaction that changes the items).

The difference between good and bad denormalization is intent: you know exactly which copy is the source of truth and how the others are kept up to date.

Tip: a quick smell test for any table design — if you’d have to update more than one row to change one real-world fact, the design is repeating that fact.

Exercise

Split a spreadsheet into tables

The starter creates workshop_signups, a flat table copied from a spreadsheet: every signup repeats the attendee's email and name. Normalise the attendees out of it. Create a table attendees with id (integer identity primary key), email (text, required, unique) and name (text, required). Fill it with one row per distinct attendee using INSERT … SELECT DISTINCT, ordered by email so the ids are predictable. Then write a query that joins workshop_signups to attendees on email and returns each attendee's id, name and number of signups, ordered by id.

SQL
CREATE TABLE workshop_signups (
  signup_id      integer PRIMARY KEY,
  attendee_email text NOT NULL,
  attendee_name  text NOT NULL,
  workshop       text NOT NULL
);
INSERT INTO workshop_signups VALUES
  (1, 'mira@example.com', 'Mira Shah',   'Latte Art'),
  (2, 'leo@example.com',  'Leo Park',    'Latte Art'),
  (3, 'mira@example.com', 'Mira Shah',   'Tea Tasting'),
  (4, 'ana@example.com',  'Ana Silva',   'Home Roasting'),
  (5, 'mira@example.com', 'Mira Shah',   'Home Roasting');

-- 1. the attendees table  2. fill it  3. signups per attendee

Summary

  • Normalization means organising tables so each fact is stored in exactly one place.
  • Repeated facts cause anomalies: updates that miss copies, data you can't insert, and facts lost when rows are deleted.
  • 1NF: one value per cell, no lists or repeating column groups. 2NF and 3NF: every column depends on the key, the whole key, and nothing but the key.
  • Normalising means splitting a wide table into narrower ones linked by keys; joins put them back together.
  • Denormalise deliberately — for history (an order's unit_price), reporting or performance — never by accident.