SQL course · Module 8: Database design · Lesson 29 of 40
Quick answer: Database normalization means splitting a table so that each fact is stored in one place only. First normal form (1NF) puts one value in each cell. Second normal form (2NF) removes columns that depend on only part of a composite key. Third normal form (3NF) removes columns that depend on another non-key column. Boyce-Codd normal form (BCNF) goes one step further: in every rule of the form “A decides B”, the left side A must be a key (or contain one).
In this lesson you will learn:
- what a functional dependency is, in plain words, and how to check one with SQL
- the update, insert and delete anomalies a badly designed table causes
- how to take a flat table to 1NF, 2NF, 3NF and BCNF with real CREATE TABLE statements
- how to prove with a JOIN that no data was lost
Before you start: Lesson 28, Database Design: ER Diagrams, Entities, Relationships and Keys and Lesson 19, SQL Transactions: BEGIN, COMMIT, ROLLBACK and SAVEPOINT.
We ran every example on PostgreSQL 16.15 with the shop database and checked the current PostgreSQL 18 manual. Everything here works on newer versions too. This lesson builds practice tables from the shop data and drops them at the end, so the four shop tables stay unchanged.
Table of Contents
Functional dependencies in plain words
A functional dependency A -> B means: if you know A, you know B. Two rows with the same A always have the same B. The textbook Database System Concepts defines it the same way: whenever two rows “agree on the attributes α, they also agree on the attributes β” (Silberschatz, Korth and Sudarshan, chapter 7 slides).
In the shop: order_id -> customer_id (an order has one customer), customer_id -> name, city, and product_id -> name, category. Every normal form is a rule about these dependencies, so we need a table that breaks them.
Database normalization starts with a bad table
Not even 1NF: a list inside a cell
Imagine the shop started with a spreadsheet: one row per order, and all products typed into one cell.
CREATE TABLE order_sheet AS
SELECT o.id AS order_id, o.order_date, c.name AS customer,
string_agg(p.name, ', ' ORDER BY p.name) AS products
FROM orders o
JOIN customers c ON c.id = o.customer_id
JOIN order_items oi ON oi.order_id = o.id
JOIN products p ON p.id = oi.product_id
GROUP BY o.id, o.order_date, c.name;
SELECT * FROM order_sheet WHERE order_id IN (13, 14) ORDER BY order_id;
SELECT 22
order_id | order_date | customer | products
----------+------------+-------------+---------------------------------------------------
13 | 2026-08-25 | Sofia Rossi | USB-C Hub, Wireless Mouse
14 | 2026-09-02 | bilal ahmed | Laptop Stand, Mechanical Keyboard, Wireless Mouse
(2 rows)
How many orders contain the Wireless Mouse?
SELECT count(*) AS exact_match FROM order_sheet WHERE products = 'Wireless Mouse';
SELECT count(*) AS like_match FROM order_sheet WHERE products LIKE '%Wireless Mouse%';
exact_match
-------------
0
(1 row)
like_match
------------
6
(1 row)
The normal = finds nothing, because no cell holds only that name. LIKE works here by luck, but there is no place for the quantity, no foreign key can check the names, and a typo would never be caught. 1NF says each cell holds one indivisible value; the textbook calls such values “atomic”. William Kent’s guide says the same: 1NF “excludes variable repeating fields and groups” (Kent: A Simple Guide to Five Normal Forms).
1NF: one row per order line
CREATE TABLE order_lines_flat (
order_id integer NOT NULL,
order_date date NOT NULL,
status text NOT NULL,
customer_id integer NOT NULL,
customer_name text NOT NULL,
customer_city text,
product_id integer NOT NULL,
product_name text NOT NULL,
category text NOT NULL,
quantity integer NOT NULL,
unit_price numeric(10,2) NOT NULL,
PRIMARY KEY (order_id, product_id)
);
INSERT INTO order_lines_flat
SELECT o.id, o.order_date, o.status, c.id, c.name, c.city,
p.id, p.name, p.category, oi.quantity, oi.unit_price
FROM order_items oi
JOIN orders o ON o.id = oi.order_id
JOIN customers c ON c.id = o.customer_id
JOIN products p ON p.id = oi.product_id;
CREATE TABLE
INSERT 0 40
This table is in 1NF, with the key (order_id, product_id). Look at Ayesha’s rows:
SELECT order_id, customer_name, customer_city, product_name, quantity, unit_price
FROM order_lines_flat
WHERE customer_id = 1
ORDER BY order_id, product_id;
order_id | customer_name | customer_city | product_name | quantity | unit_price
----------+---------------+---------------+-----------------------------+----------+------------
1 | Ayesha Malik | Lahore | Wireless Mouse | 1 | 22.99
1 | Ayesha Malik | Lahore | Notebook Pack | 2 | 12.50
4 | Ayesha Malik | Lahore | Laptop Stand | 1 | 45.00
11 | Ayesha Malik | Lahore | Desk Lamp | 1 | 32.00
11 | Ayesha Malik | Lahore | Noise-Cancelling Headphones | 1 | 149.00
17 | Ayesha Malik | Lahore | Desk Lamp | 2 | 32.00
17 | Ayesha Malik | Lahore | Gel Pens 10-Pack | 4 | 6.75
(7 rows)
“Ayesha lives in Lahore” is stored 7 times. That repetition is the real problem.
Check a dependency with SQL
A dependency A -> B holds in the data when no value of A has two different values of B. GROUP BY with HAVING (Lesson 10) finds the breaks:
SELECT order_id
FROM order_lines_flat
GROUP BY order_id
HAVING COUNT(DISTINCT customer_name) > 1;
SELECT product_id, product_name, COUNT(DISTINCT unit_price) AS prices
FROM order_lines_flat
GROUP BY product_id, product_name
HAVING COUNT(DISTINCT unit_price) > 1;
order_id
----------
(0 rows)
product_id | product_name | prices
------------+----------------+--------
1 | Wireless Mouse | 2
(1 row)
order_id -> customer_name holds. product_id -> unit_price does not: the mouse was sold at 22.99 and later at 24.99. So unit_price depends on the whole key (this product in this order). Data can only show that a rule is broken; whether a rule must always hold is a business decision.
Update, insert and delete anomalies
We run the first and last demos inside BEGIN ... ROLLBACK so the table stays as it is.
Update anomaly. Ayesha moves to Islamabad, and someone updates only her latest order:
BEGIN;
UPDATE order_lines_flat SET customer_city = 'Islamabad' WHERE order_id = 17;
SELECT DISTINCT customer_name, customer_city FROM order_lines_flat WHERE customer_id = 1;
ROLLBACK;
BEGIN
UPDATE 2
customer_name | customer_city
---------------+---------------
Ayesha Malik | Islamabad
Ayesha Malik | Lahore
(2 rows)
ROLLBACK
Now the database says two different things about one person.
Insert anomaly. The shop adds a product nobody has ordered yet:
INSERT INTO order_lines_flat (product_id, product_name, category)
VALUES (11, 'Monitor Arm', 'Accessories');
ERROR: null value in column "order_id" of relation "order_lines_flat" violates not-null constraint
DETAIL: Failing row contains (null, null, null, null, null, null, 11, Monitor Arm, Accessories, null, null).
A product cannot exist without an order. That is why the Ergonomic Chair and Omar Haddad (no orders) are missing from this table already: it holds 9 customers and 9 products, not 10 and 10.
Delete anomaly. Daniel Weber asks us to delete his orders:
BEGIN;
DELETE FROM order_lines_flat WHERE customer_id = 6;
SELECT COUNT(*) AS rows_about_daniel FROM order_lines_flat WHERE customer_name = 'Daniel Weber';
ROLLBACK;
BEGIN
DELETE 4
rows_about_daniel
-------------------
0
(1 row)
ROLLBACK
Deleting his orders also deleted Daniel. All three anomalies have the same cause: one table stores facts about several different things.

2NF: no partial dependencies
2NF = 1NF, and no non-key column (a column that is not part of any candidate key) depends on only part of a key. Course notes put it as “every non-prime attribute is fully dependent on each candidate key” (NUS CS4221: Normal Forms). Edgar F. Codd defined 2NF in 1971 (Wikipedia: Second normal form). Our key is (order_id, product_id), but order_date, status and the customer columns depend on order_id alone, and product_name and category on product_id alone. Each group moves to its own table:
CREATE TABLE nf_orders (
order_id integer PRIMARY KEY,
order_date date NOT NULL,
status text NOT NULL,
customer_id integer NOT NULL,
customer_name text NOT NULL,
customer_city text
);
CREATE TABLE nf_products (
product_id integer PRIMARY KEY,
product_name text NOT NULL,
category text NOT NULL
);
CREATE TABLE nf_order_lines (
order_id integer REFERENCES nf_orders (order_id),
product_id integer REFERENCES nf_products (product_id),
quantity integer NOT NULL,
unit_price numeric(10,2) NOT NULL,
PRIMARY KEY (order_id, product_id)
);
We fill them with INSERT ... SELECT DISTINCT from the flat table (Lesson 17):
INSERT INTO nf_orders
SELECT DISTINCT order_id, order_date, status, customer_id, customer_name, customer_city
FROM order_lines_flat;
INSERT INTO nf_products
SELECT DISTINCT product_id, product_name, category
FROM order_lines_flat;
INSERT INTO nf_order_lines
SELECT order_id, product_id, quantity, unit_price
FROM order_lines_flat;
INSERT INTO nf_products VALUES (11, 'Monitor Arm', 'Accessories');
INSERT 0 22
INSERT 0 9
INSERT 0 40
INSERT 0 1
40 lines became 22 orders and 9 products, and the new Monitor Arm now fits without an order.
3NF: no transitive dependencies
3NF = 2NF, and no non-key column depends on another non-key column. nf_orders still breaks it: order_id -> customer_id -> customer_name, customer_city. The name depends on the key only through customer_id, a chain called a transitive dependency (NUS CS4221: Normal Forms). Ayesha’s city is still stored 4 times, once per order. Move the customer facts out and link with a foreign key:
CREATE TABLE nf_customers (
customer_id integer PRIMARY KEY,
customer_name text NOT NULL,
customer_city text
);
INSERT INTO nf_customers
SELECT DISTINCT customer_id, customer_name, customer_city
FROM nf_orders;
ALTER TABLE nf_orders
DROP COLUMN customer_name,
DROP COLUMN customer_city,
ADD FOREIGN KEY (customer_id) REFERENCES nf_customers (customer_id);
SELECT * FROM nf_orders WHERE customer_id = 1 ORDER BY order_id;
CREATE TABLE
INSERT 0 9
ALTER TABLE
order_id | order_date | status | customer_id
----------+------------+-----------+-------------
1 | 2026-06-03 | delivered | 1
4 | 2026-06-22 | cancelled | 1
11 | 2026-08-11 | delivered | 1
17 | 2026-09-29 | delivered | 1
(4 rows)
These four tables have the same shape as the real shop tables. William Kent summed up 2NF and 3NF in 1983: a non-key column must give a fact about the key, the whole key, and nothing but the key (Kent).
Check that a JOIN rebuilds the original rows
A split is safe only if nothing is lost or invented. We save the four-table join as a view (Lesson 23) and compare it with the flat table using EXCEPT (Lesson 15) in both directions:
CREATE VIEW rebuilt AS
SELECT ol.order_id, o.order_date, o.status, o.customer_id, c.customer_name, c.customer_city,
ol.product_id, p.product_name, p.category, ol.quantity, ol.unit_price
FROM nf_order_lines ol
JOIN nf_orders o ON o.order_id = ol.order_id
JOIN nf_customers c ON c.customer_id = o.customer_id
JOIN nf_products p ON p.product_id = ol.product_id;
SELECT (SELECT COUNT(*) FROM order_lines_flat) AS original_rows,
(SELECT COUNT(*) FROM rebuilt) AS rebuilt_rows,
(SELECT COUNT(*) FROM (SELECT * FROM order_lines_flat EXCEPT SELECT * FROM rebuilt) AS a) AS missing,
(SELECT COUNT(*) FROM (SELECT * FROM rebuilt EXCEPT SELECT * FROM order_lines_flat) AS b) AS extra;
CREATE VIEW
original_rows | rebuilt_rows | missing | extra
---------------+--------------+---------+-------
40 | 40 | 0 | 0
(1 row)
Same 40 rows, none missing, none extra. Textbooks call a split that never loses information a lossless decomposition; this check confirms it for our data.
BCNF: every determinant is a key
BCNF is stricter: for every dependency A -> B (where B is not part of A), A must be a superkey, that is, a key or a key plus extra columns. 3NF allows one exception, when B is part of some candidate key, and BCNF removes it (db-book slides). Tables that are in 3NF but not BCNF are rare, so here is a made-up support desk: each customer gets one agent per product category, and each agent works on one category.
CREATE TABLE support_assignments (
customer_id integer NOT NULL,
category text NOT NULL,
agent text NOT NULL,
PRIMARY KEY (customer_id, category)
);
INSERT INTO support_assignments VALUES
(1, 'Electronics', 'Sara'),
(1, 'Stationery', 'Nadia'),
(2, 'Electronics', 'Sara'),
(3, 'Electronics', 'Tom'),
(4, 'Stationery', 'Nadia');
The rule agent -> category has agent on the left, which is not a key, so the table is not in BCNF. It is in 3NF, because category is part of the key. Nothing stops Sara from getting a second category:
BEGIN;
INSERT INTO support_assignments VALUES (5, 'Stationery', 'Sara');
SELECT agent, COUNT(DISTINCT category) AS categories
FROM support_assignments
GROUP BY agent
HAVING COUNT(DISTINCT category) > 1;
ROLLBACK;
BEGIN
INSERT 0 1
agent | categories
-------+------------
Sara | 2
(1 row)
ROLLBACK
The BCNF split gives the agent rule its own table, where agent is the key. Then we run the same kind of EXCEPT check:
CREATE TABLE agents (
agent text PRIMARY KEY,
category text NOT NULL
);
CREATE TABLE customer_agents (
customer_id integer NOT NULL,
agent text NOT NULL REFERENCES agents (agent),
PRIMARY KEY (customer_id, agent)
);
INSERT INTO agents SELECT DISTINCT agent, category FROM support_assignments;
INSERT INTO customer_agents SELECT customer_id, agent FROM support_assignments;
SELECT (SELECT COUNT(*) FROM (SELECT * FROM support_assignments
EXCEPT
SELECT ca.customer_id, a.category, ca.agent
FROM customer_agents ca JOIN agents a ON a.agent = ca.agent) AS x) AS missing,
(SELECT COUNT(*) FROM customer_agents) AS rows;
CREATE TABLE
CREATE TABLE
INSERT 0 3
INSERT 0 5
missing | rows
---------+------
0 | 5
(1 row)
Sara, Tom and Nadia each appear once in agents, and the join gives back all 5 rows. Now the key enforces the agent rule:
INSERT INTO agents VALUES ('Sara', 'Stationery');
ERROR: duplicate key value violates unique constraint "agents_pkey"
DETAIL: Key (agent)=(Sara) already exists.
There is a price, though: the old key (customer_id, category) is gone, so a customer can now get two Electronics agents:
INSERT INTO customer_agents VALUES (1, 'Tom');
SELECT ca.customer_id, a.category, string_agg(ca.agent, ', ' ORDER BY ca.agent) AS agents
FROM customer_agents ca
JOIN agents a ON a.agent = ca.agent
GROUP BY ca.customer_id, a.category
HAVING COUNT(*) > 1;
INSERT 0 1
customer_id | category | agents
-------------+-------------+-----------
1 | Electronics | Sara, Tom
(1 row)
The textbook warns about exactly this: “It is not always possible to achieve both BCNF and dependency preservation.” In practice, many designs stop at 3NF, and Microsoft’s guide calls 3NF “the highest level necessary for most applications” (Microsoft Learn).
Try it yourself
Exercise 1: Check a dependency
In order_lines_flat, does customer_id -> customer_city hold? Write the GROUP BY ... HAVING check.
Exercise 2: Rebuild one order
Use the rebuilt view to show order 14 with customer name, product, quantity and unit price.
Exercise 3: The update anomaly is gone
Inside a transaction, change Ayesha’s city once in nf_customers, then list her distinct name and city from rebuilt. Roll back.
Solutions
Solution 1
SELECT customer_id
FROM order_lines_flat
GROUP BY customer_id
HAVING COUNT(DISTINCT customer_city) > 1;
customer_id
-------------
(0 rows)
No customer has two cities, so the dependency holds in this data.
Solution 2
SELECT order_id, customer_name, product_name, quantity, unit_price
FROM rebuilt
WHERE order_id = 14
ORDER BY product_id;
order_id | customer_name | product_name | quantity | unit_price
----------+---------------+---------------------+----------+------------
14 | bilal ahmed | Wireless Mouse | 1 | 24.99
14 | bilal ahmed | Mechanical Keyboard | 1 | 89.00
14 | bilal ahmed | Laptop Stand | 1 | 45.00
(3 rows)
The joins put the three lines of order 14 back together.
Solution 3
BEGIN;
UPDATE nf_customers SET customer_city = 'Islamabad' WHERE customer_id = 1;
SELECT DISTINCT customer_name, customer_city FROM rebuilt WHERE customer_id = 1;
ROLLBACK;
BEGIN
UPDATE 1
customer_name | customer_city
---------------+---------------
Ayesha Malik | Islamabad
(1 row)
ROLLBACK
One row changed, and every order sees the new city. When you finish, drop the practice objects:
DROP VIEW rebuilt;
DROP TABLE nf_order_lines, nf_orders, nf_customers, nf_products,
order_lines_flat, order_sheet,
customer_agents, agents, support_assignments;
DROP VIEW
DROP TABLE
Common mistakes
- Storing lists in one column.
products = 'Wireless Mouse'found 0 of 6 orders. Use one row per item. - Splitting on dependencies that are not real.
product_id -> unit_pricelooks true until the price changes. Ask what the business rule is, not only what today’s data shows. - Forgetting the lossless check. After a split, compare the JOIN with the original using
EXCEPTboth ways. - Moving columns before the new table has a key. Add the
PRIMARY KEYand theFOREIGN KEY, or duplicates come back. - Expecting BCNF to be free. Our split allowed
Sara, Tomfor one customer and category. Check which rules the new keys no longer enforce.
Frequently asked questions
What is the difference between 3NF and BCNF?
In 3NF, a non-key column may not depend on anything but the key, but a dependency is allowed if its right side is part of a candidate key. BCNF removes that exception: the left side of every dependency must be a key (or contain one). Only rare 3NF tables fail BCNF (Wikipedia: Boyce-Codd normal form).
Is normalization always good?
It removes repeated facts and the anomalies they cause, which matters most for tables that change often. The price is more tables and more joins, so reporting systems often copy data into a less normalized shape. Lesson 30 covers that trade-off.
Do I need 4NF and 5NF?
4NF (Ronald Fagin, 1977) deals with multivalued dependencies, such as two independent lists about one customer (Kent), and tables that pass 4NF but fail 5NF are rare (Wikipedia: Fourth normal form). For most application tables, 3NF or BCNF is the target.
Summary
- A functional dependency
A -> Bmeans rows with the same A always have the same B;GROUP BY ... HAVINGfinds breaks. - A flat table causes update, insert and delete anomalies because it mixes facts about several things.
- 1NF: one value per cell. 2NF: no dependency on part of the key. 3NF: no dependency between non-key columns.
- BCNF: every determinant is a key; it can cost you a rule the old key enforced.
- After each split, check with a JOIN and
EXCEPTthat the original rows come back exactly.
Next lesson
Next: Lesson 30, Normalization vs Denormalization: OLTP vs Analytics.
Sources
- Database System Concepts (Silberschatz, Korth, Sudarshan), chapter 7 slides: functional dependencies, 1NF, 3NF, BCNF, lossless decomposition
- NUS CS4221 lecture notes: Normal Forms (2NF, 3NF, BCNF)
- William Kent: A Simple Guide to Five Normal Forms in Relational Database Theory (1983)
- Microsoft Learn: Description of the database normalization basics
- Wikipedia: Second normal form
- Wikipedia: Boyce-Codd normal form
- Wikipedia: Fourth normal form
- PostgreSQL documentation: Constraints





