Spotting Anomalies and Normalizing to 3NF
Load a spreadsheet-shaped order report into PostgreSQL, watch the insert, update and delete anomalies corrupt it, then normalise to third normal form and re-test every anomaly.
Contentsand lesson path
On this page
- Goal and setup
- Step 1: one value per cell (1NF)
- Step 2: break it three ways
- Step 3: name the cause — functional dependencies
- Step 4: remove the partial dependencies (2NF)
- Step 5: remove the transitive dependencies (3NF)
- Step 6: re-test every anomaly
- Troubleshooting
- Check your understanding
- Apply it
- Review prompt
Lessons of Relational Data Modeling
- From Requirements to Entities and Relationships Concept · 20 min
- Choosing Keys: Natural, Surrogate and Composite Concept · 20 min
- Worked Example: An Order Management Schema Worked example · 30 min
- Spotting Anomalies and Normalizing to 3NF Lab · 30 min
- Constraints as Executable Business Rules Lab · 25 min
- Check: Review and Repair a Flawed Schema Knowledge check · 8 questions
What you will learn
- Normalize a schema to third normal form by identifying functional dependencies and the insert, update and delete anomalies they cause.
Prerequisites: SQL Fundamentals
01. Goal and setup
Every lesson so far designed a schema from scratch. Most real work repairs one instead, because most data arrives the way spreadsheets export it: one wide table, facts repeated on every row. This lab takes exactly such a table, breaks it three ways on purpose, then walks it up to third normal form (3NF) until nothing breaks.
You need: the rdm-lab container from the keys lesson and a psql session in it (docker exec -it rdm-lab psql -U postgres -d store). This lab was run against PostgreSQL 17.12; any 17.x behaves the same. The input is the weekly sales report:
order_ref | placed_on | customer | items
----------+------------+------------------+----------------------------------
ORD-1001 | 2026-10-20 | [email protected] | USB-C cable 1m x1; HDMI cable 2m x2
ORD-1002 | 2026-10-21 | [email protected] | Desk mat x1
ORD-1003 | 2026-10-22 | [email protected] | USB-C cable 1m x3; HDMI cable 2m x2Give the exercise a database of its own so it cannot collide with the order schema:
DROP DATABASE IF EXISTS reportdb;
CREATE DATABASE reportdb;
\c reportdbNOTICE: database "reportdb" does not exist, skipping
DROP DATABASE
CREATE DATABASE
You are now connected to database "reportdb" as user "postgres".02. Step 1: one value per cell (1NF)
First normal form is the rule that every cell holds one atomic value — no lists, no repeated groups. The items cell breaks it: it holds two products, and no SQL can cleanly ask "which orders included a desk mat?". The import fixes that by splitting each cell into its own row, and each line needs its own identity within the order, which arrives as a composite key (order_ref, line):
CREATE TABLE order_report (
order_ref text NOT NULL,
line integer NOT NULL,
placed_on date NOT NULL,
customer_email text NOT NULL,
customer_name text NOT NULL,
product_sku text NOT NULL,
product_name text NOT NULL,
quantity integer NOT NULL,
unit_price numeric(10,2) NOT NULL,
PRIMARY KEY (order_ref, line)
);
INSERT INTO order_report VALUES
('ORD-1001', 1, '2026-10-20', '[email protected]', 'Ana Petrova',
'CBL-USBC-1M', 'USB-C cable 1m', 1, 9.90),
('ORD-1001', 2, '2026-10-20', '[email protected]', 'Ana Petrova',
'CBL-HDMI-2M', 'HDMI cable 2m', 2, 12.50),
('ORD-1002', 1, '2026-10-21', '[email protected]', 'Ana Petrova',
'PAD-STD', 'Desk mat', 1, 18.00),
('ORD-1003', 1, '2026-10-22', '[email protected]', 'Liu Chen',
'CBL-USBC-1M', 'USB-C cable 1m', 3, 8.90),
('ORD-1003', 2, '2026-10-22', '[email protected]', 'Liu Chen',
'CBL-HDMI-2M', 'HDMI cable 2m', 2, 11.25);CREATE TABLE
INSERT 0 5SELECT order_ref, line, customer_email, product_sku, product_name, unit_price
FROM order_report
ORDER BY order_ref, line; order_ref | line | customer_email | product_sku | product_name | unit_price
-----------+------+-----------------+-------------+----------------+------------
ORD-1001 | 1 | [email protected] | CBL-USBC-1M | USB-C cable 1m | 9.90
ORD-1001 | 2 | [email protected] | CBL-HDMI-2M | HDMI cable 2m | 12.50
ORD-1002 | 1 | [email protected] | PAD-STD | Desk mat | 18.00
ORD-1003 | 1 | [email protected] | CBL-USBC-1M | USB-C cable 1m | 8.90
ORD-1003 | 2 | [email protected] | CBL-HDMI-2M | HDMI cable 2m | 11.25
(5 rows)03. Step 2: break it three ways
The table is in 1NF and already wrong, because facts that belong to one thing are stored on rows about other things. Prove it with three experiments.
Update anomaly. The supplier renames the cable to "USB-C cable 1m (2027)". Someone updates one row of the report and misses the other:
UPDATE order_report SET product_name = 'USB-C cable 1m (2027)'
WHERE order_ref = 'ORD-1003' AND product_sku = 'CBL-USBC-1M';
SELECT order_ref, product_sku, product_name
FROM order_report
WHERE product_sku = 'CBL-USBC-1M'
ORDER BY order_ref;UPDATE 1
order_ref | product_sku | product_name
-----------+-------------+-----------------------
ORD-1001 | CBL-USBC-1M | USB-C cable 1m
ORD-1003 | CBL-USBC-1M | USB-C cable 1m (2027)
(2 rows)One product, two names, and no constraint can help: both spellings are legal strings. A fact stored in many rows is updated correctly only if every writer is perfect forever.
Insert anomaly. Marketing wants to record a new customer before her first order:
INSERT INTO order_report (order_ref, line, placed_on, customer_email, customer_name)
VALUES (NULL, NULL, NULL, '[email protected]', 'Mara Ionescu');ERROR: null value in column "order_ref" of relation "order_report"
violates not-null constraint
DETAIL: Failing row contains (null, null, null, [email protected],
Mara Ionescu, null, null, null, null).The row has no identity without an order, so the customer cannot exist without one. The same applies to a new product not yet sold: the table cannot hold a fact about the catalogue, only about sales.
Delete anomaly. ORD-1002 was a staff test order and must go:
DELETE FROM order_report WHERE order_ref = 'ORD-1002';
SELECT DISTINCT product_sku, product_name
FROM order_report
ORDER BY product_sku, product_name;DELETE 1
product_sku | product_name
-------------+-----------------------
CBL-HDMI-2M | HDMI cable 2m
CBL-USBC-1M | USB-C cable 1m
CBL-USBC-1M | USB-C cable 1m (2027)
(3 rows)The desk mat has vanished from the shop. Its only home was a row about an order, and deleting the order deleted the product. Notice the catalogue listing now also shows the stale rename: errors compound.
04. Step 3: name the cause — functional dependencies
A functional dependency X → Y says: whenever two rows agree on X, they must agree on Y — knowing X fixes Y. Write down what the data actually depends on:
(order_ref, line) → product_sku, the whole key: one line, one fact
quantity, unit_price
order_ref → placed_on, part of the key fixes order facts
customer_email,
customer_name
customer_email → customer_name a non-key column fixes another
product_sku → product_name a non-key column fixes anotherThree kinds are listed, and only the first belongs here. product_sku, quantity and unit_price depend on the whole key — change either part of the key and they can differ, which is exactly what a row should be. The second kind is a partial dependency: placed_on and the customer columns depend on order_ref alone, a part of the composite key, so they repeat on every line of the order. The third kind is a transitive dependency: customer_name and product_name depend on the key only through another non-key column — key → product_sku → product_name, and order_ref → customer_email → customer_name.
Each dependency type maps straight onto an anomaly: partial dependencies repeat order facts line by line (update anomaly when the order changes), and transitive dependencies repeat facts about customers and products (update and delete anomalies when those change). Second normal form forbids partial dependencies; third normal form additionally forbids transitive ones. The slogan worth memorising: every non-key column must depend on the key, the whole key, and nothing but the key.
05. Step 4: remove the partial dependencies (2NF)
Move the order-level facts to a table keyed on order_ref alone. The import copies each distinct order once, and the lines keep only line-level facts. The report keeps its readable keys — order references, emails, SKUs — so the change is visible and reversible; swapping in surrogate keys is the keys lesson's job, not this one.
CREATE TABLE orders (
order_ref text PRIMARY KEY,
placed_on date NOT NULL,
customer_email text NOT NULL,
customer_name text NOT NULL
);
CREATE TABLE order_lines (
order_ref text NOT NULL,
line integer NOT NULL,
product_sku text NOT NULL,
product_name text NOT NULL,
quantity integer NOT NULL,
unit_price numeric(10,2) NOT NULL,
PRIMARY KEY (order_ref, line)
);
INSERT INTO orders
SELECT DISTINCT order_ref, placed_on, customer_email, customer_name
FROM order_report;
INSERT INTO order_lines
SELECT order_ref, line, product_sku, product_name, quantity, unit_price
FROM order_report;
DROP TABLE order_report;CREATE TABLE
CREATE TABLE
INSERT 0 2
INSERT 0 4
DROP TABLETwo orders, four lines — no rows lost or invented. Order facts now live once per order. But 2NF is not the finish line; prove it by retrying the rename:
UPDATE order_lines SET product_name = 'USB-C cable 1m (2027)'
WHERE product_sku = 'CBL-USBC-1M';UPDATE 2Still two rows, because product_name depends on product_sku, and that transitive dependency survived the 2NF split. The same is true of customer_name behind customer_email in orders.
06. Step 5: remove the transitive dependencies (3NF)
Give each transitive fact its own table, keyed by the column it actually depends on, then remove the repeated column and point a foreign key at the new table:
CREATE TABLE customers (
email text PRIMARY KEY,
full_name text NOT NULL
);
CREATE TABLE products (
sku text PRIMARY KEY,
name text NOT NULL
);
INSERT INTO customers
SELECT DISTINCT customer_email, customer_name FROM orders;
INSERT INTO products
SELECT DISTINCT product_sku, product_name FROM order_lines;
ALTER TABLE orders
DROP COLUMN customer_name,
ADD CONSTRAINT orders_customer_fkey
FOREIGN KEY (customer_email) REFERENCES customers(email);
ALTER TABLE order_lines
DROP COLUMN product_name,
ADD CONSTRAINT order_lines_order_fkey
FOREIGN KEY (order_ref) REFERENCES orders(order_ref),
ADD CONSTRAINT order_lines_product_fkey
FOREIGN KEY (product_sku) REFERENCES products(sku);CREATE TABLE
CREATE TABLE
INSERT 0 2
INSERT 0 2
ALTER TABLE
ALTER TABLEPopulate before adding the foreign keys, because the constraint checks existing rows the moment it is added — an order whose customer is missing would abort the ALTER TABLE. The delete anomaly already bit here, by the way: the desk mat was erased back in step 2, so it never reached products. Data lost to a bad design does not come back with a better one; it needs re-entry.
07. Step 6: re-test every anomaly
Run the three failing scenarios again, then rebuild the report from the normalised tables to prove nothing was lost:
INSERT INTO customers (email, full_name)
VALUES ('[email protected]', 'Mara Ionescu');
UPDATE products SET name = 'USB-C cable 1m (2027)'
WHERE sku = 'CBL-USBC-1M';
INSERT INTO products (sku, name) VALUES ('PAD-STD', 'Desk mat');INSERT 0 1
UPDATE 1
INSERT 0 1A customer with no order, a product renamed in exactly one row, and a product with no sales — all three impossible in the flat table, all routine now. Reconstruct the report:
SELECT o.order_ref, c.full_name, l.line, p.name, l.quantity, l.unit_price
FROM orders o
JOIN customers c ON c.email = o.customer_email
JOIN order_lines l ON l.order_ref = o.order_ref
JOIN products p ON p.sku = l.product_sku
ORDER BY o.order_ref, l.line; order_ref | full_name | line | name | quantity | unit_price
-----------+-------------+------+-----------------------+----------+------------
ORD-1001 | Ana Petrova | 1 | USB-C cable 1m (2027) | 1 | 9.90
ORD-1001 | Ana Petrova | 2 | HDMI cable 2m | 2 | 12.50
ORD-1003 | Liu Chen | 1 | USB-C cable 1m (2027) | 3 | 8.90
ORD-1003 | Liu Chen | 2 | HDMI cable 2m | 2 | 11.25
(4 rows)DELETE FROM order_lines WHERE order_ref = 'ORD-1001';
DELETE FROM orders WHERE order_ref = 'ORD-1001';
SELECT sku, name FROM products ORDER BY sku;DELETE 2
DELETE 1
sku | name
-------------+-----------------------
CBL-HDMI-2M | HDMI cable 2m
CBL-USBC-1M | USB-C cable 1m (2027)
PAD-STD | Desk mat
(3 rows)ORD-1001 is gone and every product survives it — the delete anomaly is closed, and the first delete had to run before the second because order_lines now references orders, which is the constraints lesson's territory.
08. Troubleshooting
09. Check your understanding
10. Apply it
Run the same pipeline on the clinic practice domain:
-
Write the one-table export a clinic spreadsheet would produce for bookings (patient, doctor, appointment time, treatment name and price, all on one row per booking) and load it into a fresh database.
-
Trigger each of the three anomalies with a statement, capturing the output the way step 2 did.
-
List the functional dependencies, mark each as whole-key, partial or transitive, and split to 3NF.
-
Re-run the three failing statements to show they now succeed, and rebuild the original report with a join.
There is a stricter form just beyond 3NF, Boyce-Codd normal form (BCNF, sometimes called 3.5NF), which requires every determinant to be a candidate key and differs from 3NF only for rare shapes with overlapping candidate keys; fourth and fifth normal form go further still, dealing with multi-valued and join dependencies. Recognising when BCNF applies is a good question to keep in the note you hand to review.
11. Review prompt
Without looking back: define a functional dependency in one sentence, with an example from the lab. Name the three anomalies and match each to the dependency type that causes it. What exactly does 2NF forbid, and why did the lab's 2NF table still fail the rename test? Finish the slogan: every non-key column depends on the key, the whole key, and — what?
References
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.