normal forms, 1NF to BCNF
one fact, one place. update, insert and delete anomalies, and the decomposition that cures them.
- address
- everything.design.normal_forms
- part
- part 7, schema design
- chapter
- chapter 6 of 13
- reading time
- 13 min
one fact, one place. when a table stores the same fact in many rows (a customer's address on every order line), the copies can disagree. that's the update anomaly, and it comes with two siblings: you can't store a fact until something unrelated exists (insert), and deleting one thing deletes another (delete).
the normal forms are a checklist for finding those copies. 3NF (or BCNF, which is nearly always the same table) is the everyday target. the cure is always the same move: split the table along a functional dependency.
the picture
you know the spreadsheet everyone starts with? one row per order line, and every row spells out the customer's name, email and address, because that's what the invoice needs. it reads well. it also stores Ada's address four times, and nothing in the table says those four have to match.
think of writing your friend's address on the back of every photo you've sent her (the order lines), instead of keeping it once in your address book (a customers table). the day she moves, you have to find every photo. miss one, and you now know two addresses for her, with no way to tell which is current.
| order_idint4 | skutext | producttext | customertext | addresstext | |
|---|---|---|---|---|---|
| 101 | MUG-02 | blue mug | Ada Lovelace | ||
| 101 | TEA-01 | assam tea | Ada Lovelace | ||
| 102 | TEA-01 | assam tea | Ada Lovelace | ||
| 103 | TEA-02 | green tea | Grace Okafor | ||
| 104 | MUG-02 | blue mug | Ada Lovelace |
every order line carries a copy of the address. edit one copy (or press × to delete an order line, or add a customer who hasn't ordered).
the lab edits rows in your browser. the three anomalies are real postgres behaviour though, so here they are on the engine.
three anomalies, for real
the wide table, keyed by (order_id, sku), the way the lab shows it:
CREATE TABLE orders_wide (
order_id int NOT NULL,
sku text NOT NULL,
qty int NOT NULL,
product text NOT NULL,
list_price numeric(8,2) NOT NULL,
ordered_on date NOT NULL,
email text NOT NULL,
customer text NOT NULL,
address text NOT NULL,
PRIMARY KEY (order_id, sku)
);
INSERT INTO orders_wide VALUES
(101, 'TEA-01', 2, 'assam tea', 6.50, '2026-03-02', 'ada@example.com', 'Ada Lovelace', '12 Byron St, London'),
(101, 'MUG-02', 1, 'blue mug', 9.00, '2026-03-02', 'ada@example.com', 'Ada Lovelace', '12 Byron St, London'),
(102, 'TEA-01', 1, 'assam tea', 6.50, '2026-03-03', 'ada@example.com', 'Ada Lovelace', '12 Byron St, London'),
(103, 'TEA-02', 3, 'green tea', 5.00, '2026-03-04', 'grace@example.com', 'Grace Okafor', '4 Elm Rd, Leeds'),
(104, 'MUG-02', 2, 'blue mug', 9.00, '2026-03-05', 'ada@example.com', 'Ada Lovelace', '12 Byron St, London');-- Ada moved. the support agent fixes her latest order.
UPDATE orders_wide SET address = '3 Kew Gardens, London'
WHERE order_id = 104;
SELECT DISTINCT address FROM orders_wide
WHERE email = 'ada@example.com';the update anomaly:
UPDATE orders_wide SET address = '3 Kew Gardens, London' WHERE order_id = 104;
SELECT DISTINCT address FROM orders_wide WHERE email = 'ada@example.com' ORDER BY address;| addresstext |
|---|
| 12 Byron St, London |
| 3 Kew Gardens, London |
UPDATE 1, and two answers. the fix "just update every row with her email" works until the day someone forgets, or two people edit at once, or the email itself is the thing that changed.
the insert anomaly: Ming Li signs up but hasn't ordered. every row is an order line, so to store her address you need an order id and a sku, and a key column can't be NULL:
INSERT INTO orders_wide (email, customer, address)
VALUES ('ming@example.com', 'Ming Li', '9 Canal St, Manchester');the delete anomaly: Grace cancels her only order, and the shop forgets she exists. green tea goes too, since order 103 was the only row that knew its name and price:
DELETE FROM orders_wide WHERE order_id = 103;
SELECT count(*) FILTER (WHERE email = 'grace@example.com') AS grace_rows,
count(*) FILTER (WHERE sku = 'TEA-02') AS green_tea_rows
FROM orders_wide;| grace_rowsint8 | green_tea_rowsint8 |
|---|---|
| 0 | 0 |
three different symptoms, one disease: the table holds facts about several things (order lines, orders, products, customers), and each fact is stored once per row of the thing it isn't about.
the cure: split along the dependencies
a functional dependency X → Y says "rows that agree on X agree on Y". this table has four groups of them, and the key is (order_id, sku):
- key: (order_id, sku)
- ├── 1NF: one value per cellalready true here, see below
- ├──
- │ ├── sku → product, list_priceonly half the key
- │ ├── order_id → ordered_on, emailthe other half
- │ ├── products (sku, product, list_price)
- │ ├── orders (order_id, ordered_on, email, customer, address)
- │ └── order_lines (order_id, sku, qty)
- └──
- ├── email → customer, addressinside orders
- ├── customers (id, email, name, address)
- └── orders (order_id, ordered_on, customer_id)
1NF: one value per cell
first normal form asks that every cell hold one value, and that rows have a key. the classic violation is a list in a string:
CREATE TABLE orders_csv (order_id int PRIMARY KEY, skus text);
INSERT INTO orders_csv VALUES (101, 'TEA-01,MUG-02'), (102, 'TEA-01');
SELECT order_id, sku
FROM orders_csv, string_to_table(skus, ',') AS sku
ORDER BY order_id, sku;| order_idint4 | skutext |
|---|---|
| 101 | MUG-02 |
| 101 | TEA-01 |
| 102 | TEA-01 |
you can split it at read time, as above, but you can't put a foreign key on part of a string, can't index "orders containing MUG-02" with a plain b-tree, and can't stop 'TEA-01,,tea-01'. one row per item is 1NF. (schema anti-patterns has the full damage report.)
"one value" is fuzzier than it sounds. a date has a year inside it, an address has a city, and postgres happily stores arrays and jsonb. the working rule: if you'll query, constrain or join on the parts, they're separate values. if the whole thing is only ever read and written as one, it's one.
2NF: the whole key
second normal form only bites when the key has several columns. it says no non-key column may depend on part of the key. here product and list_price depend on sku alone, and ordered_on, email, customer and address on order_id alone. postgres can check the first claim: if any sku had two product names, this would return it.
SELECT sku, count(DISTINCT (product, list_price)) AS versions
FROM orders_wide
GROUP BY sku
HAVING count(DISTINCT (product, list_price)) > 1
ORDER BY sku;| skutext | versionsint8 |
|---|
no rows: sku → (product, list_price) holds in this data. so products get their own table, keyed by sku, and the line keeps only what depends on the whole (order_id, sku): qty.
3NF: nothing but the key
the orders table after 2NF still says (order_id → email) and (email → customer, address). the address depends on the key only through email, a transitive dependency. third normal form says a non-key column must depend on the key directly, so customers move out. the old mnemonic: every non-key column depends on the key, the whole key, and nothing but the key.
here's the whole decomposition, filled from the wide table (a fresh copy, before the edits above):
CREATE TABLE customers (
id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
email text NOT NULL UNIQUE,
name text NOT NULL,
address text NOT NULL
);
CREATE TABLE products (sku text PRIMARY KEY, name text NOT NULL, list_price numeric(8,2) NOT NULL);
CREATE TABLE orders (
id int PRIMARY KEY,
ordered_on date NOT NULL,
customer_id int NOT NULL REFERENCES customers
);
CREATE TABLE order_lines (
order_id int NOT NULL REFERENCES orders,
sku text NOT NULL REFERENCES products,
qty int NOT NULL,
PRIMARY KEY (order_id, sku)
);
INSERT INTO customers (email, name, address)
SELECT DISTINCT email, customer, address FROM orders_wide ORDER BY email;
INSERT INTO products SELECT DISTINCT sku, product, list_price FROM orders_wide ORDER BY sku;
INSERT INTO orders
SELECT DISTINCT w.order_id, w.ordered_on, c.id
FROM orders_wide AS w JOIN customers AS c USING (email) ORDER BY 1;
INSERT INTO order_lines SELECT order_id, sku, qty FROM orders_wide ORDER BY 1, 2;
SELECT (SELECT count(*) FROM customers) AS customers, (SELECT count(*) FROM products) AS products,
(SELECT count(*) FROM orders) AS orders, (SELECT count(*) FROM order_lines) AS lines;| customersint8 | productsint8 | ordersint8 | linesint8 |
|---|---|---|---|
| 2 | 3 | 4 | 5 |
a split is only safe if it's lossless: joining the pieces back gives exactly the original rows, no more and no fewer. it is, because every split went along a dependency whose left side became a key. ask postgres both directions of the difference:
WITH rejoined AS (
SELECT l.order_id, l.sku, l.qty, p.name AS product, p.list_price, o.ordered_on, c.email, c.name AS customer, c.address
FROM order_lines AS l
JOIN orders AS o ON o.id = l.order_id
JOIN products AS p USING (sku)
JOIN customers AS c ON c.id = o.customer_id
)
SELECT (SELECT count(*) FROM (TABLE rejoined EXCEPT TABLE orders_wide) AS x) AS extra,
(SELECT count(*) FROM (TABLE orders_wide EXCEPT TABLE rejoined) AS x) AS missing;| extraint8 | missingint8 |
|---|---|
| 0 | 0 |
zero and zero. now the three anomalies again, on the split tables. one UPDATE, one row, one address; Ming Li without an order; order 103 gone while Grace and green tea stay:
UPDATE customers SET address = '3 Kew Gardens, London' WHERE email = 'ada@example.com';
INSERT INTO customers (email, name, address) VALUES ('ming@example.com', 'Ming Li', '9 Canal St, Manchester');
DELETE FROM order_lines WHERE order_id = 103;
DELETE FROM orders WHERE id = 103;
SELECT c.name, c.address, count(o.id) AS orders
FROM customers AS c LEFT JOIN orders AS o ON o.customer_id = c.id
GROUP BY c.id
ORDER BY c.id;| nametext | addresstext | ordersint8 |
|---|---|---|
| Ada Lovelace | 3 Kew Gardens, London | 3 |
| Grace Okafor | 4 Elm Rd, Leeds | 0 |
| Ming Li | 9 Canal St, Manchester | 0 |
one level deeperthe price on the order line is not a copy
the shop dataset keeps unit_price on order_items even though products has a price, and that's correct. the product's price is "what it costs today". the line's price is "what this customer paid on that day". two different facts, so storing both is not redundancy: when the product's price changes, old orders must not. a value that looks repeated but means something different at a different time is history, and history, audit and soft delete is about keeping it on purpose.
BCNF: when a non-key decides part of the key
3NF has a loophole. it allows X → Y when Y is part of some candidate key. Boyce-Codd normal form closes it: for every non-trivial X → Y, X must be a superkey. tables that are 3NF but not BCNF are rare, and they all look alike: overlapping candidate keys. the textbook one is tutoring, where each tutor teaches one subject, and each student has one tutor per subject:
CREATE TABLE tutoring (
student text NOT NULL,
subject text NOT NULL,
tutor text NOT NULL,
PRIMARY KEY (student, subject)
);
INSERT INTO tutoring VALUES
('Maya', 'math', 'Tomás'), ('Liam', 'math', 'Yuki'),
('Maya', 'physics', 'Amara'), ('Liam', 'physics', 'Amara');
SELECT tutor, subject, count(*) AS rows FROM tutoring GROUP BY tutor, subject ORDER BY tutor;| tutortext | subjecttext | rowsint8 |
|---|---|---|
| Amara | physics | 2 |
| Tomás | math | 1 |
| Yuki | math | 1 |
tutor → subject holds, but tutor isn't a key (Amara has two rows). so "Amara teaches physics" is stored twice, and one bad UPDATE can make her teach two subjects. it's 3NF (subject is part of the key), and not BCNF.
the BCNF split is tutors(tutor → subject) and lessons(student, tutor). and here's the catch: the rule "one tutor per student per subject" spanned both columns of the old key, and now the two columns live in different tables. no key can enforce it anymore:
CREATE TABLE tutors (tutor text PRIMARY KEY, subject text NOT NULL);
CREATE TABLE lessons (student text, tutor text REFERENCES tutors, PRIMARY KEY (student, tutor));
INSERT INTO tutors SELECT DISTINCT tutor, subject FROM tutoring ORDER BY tutor;
INSERT INTO lessons SELECT student, tutor FROM tutoring ORDER BY 1, 2;
-- Maya gets a second math tutor. nothing objects.
INSERT INTO lessons VALUES ('Maya', 'Yuki');
SELECT l.student, t.subject, count(*) AS tutors
FROM lessons AS l JOIN tutors AS t USING (tutor)
GROUP BY l.student, t.subject
ORDER BY l.student, t.subject;| studenttext | subjecttext | tutorsint8 |
|---|---|---|
| Liam | math | 1 |
| Liam | physics | 1 |
| Maya | math | 2 |
| Maya | physics | 1 |
that's the trade, and it's provable: some 3NF tables can't be decomposed into BCNF without losing a dependency. you pick between redundancy (stay at 3NF, the key keeps the rule) and a rule you enforce some other way, usually a trigger. most designers stay at 3NF here, and say so in a comment.
which forms matter
| form | the rule, plainly | in practice |
|---|---|---|
| 1NF | one value per cell, rows have a key | always. arrays and jsonb are fine when the value is read and written whole |
| 2NF | non-key columns depend on the whole key | only matters with composite keys. a surrogate id key is 2NF for free, which is also how people hide 2NF problems |
| 3NF | ... and on nothing but the key | the everyday target. most anomalies you'll meet are 3NF violations |
| BCNF | every determinant is a key | the same tables as 3NF almost always. the exceptions have overlapping candidate keys |
| 4NF, 5NF | no independent multi-valued facts in one table, no join dependencies | rare. a table like (person, skill, language) storing two unrelated lists is the 4NF case |
the forms past BCNF exist and are real, but you'll recognize their problems as "this table is about two unrelated lists" long before you name them. design to 3NF, check for the BCNF case when a table has two overlapping candidate keys, and then denormalize on purpose where a measured query needs it.
keylines
- a fact stored in many rows can disagree with itself: update, insert and delete anomalies.
- split along a functional dependency, its left side becomes the new table's key, and the join is lossless.
- 2NF: the whole key. 3NF: nothing but the key. BCNF: every determinant is a key.
- 3NF is the everyday target; BCNF can cost you a constraint.
- a surrogate id doesn't normalize anything.