Sub topic 12
Normalization: Avoiding Redundancy and Update Anomalies
This chapter assumes you can read CREATE TABLE, INSERT, and SELECT ... JOIN statements. It also assumes you know what primary keys and foreign keys are. Normalization is the process of splitting data across tables so that each fact is stored in exactly one place. The goal is a design in which the database cannot contradict itself.
Why a single wide table fails
A common first design puts everything about an order into one table. It looks like a spreadsheet, so it feels natural.
CREATE TABLE order_lines_flat (
order_id INTEGER NOT NULL,
product_id INTEGER NOT NULL,
order_date DATE NOT NULL,
customer_id INTEGER NOT NULL,
customer_name VARCHAR(100) NOT NULL,
customer_email VARCHAR(255) NOT NULL,
product_name VARCHAR(100) NOT NULL,
quantity INTEGER NOT NULL CHECK (quantity > 0),
PRIMARY KEY (order_id, product_id)
);
INSERT INTO order_lines_flat VALUES
(100, 10, '2026-01-15', 1, 'Ada Lovelace', '[email protected]', 'Keyboard', 1),
(100, 11, '2026-01-15', 1, 'Ada Lovelace', '[email protected]', 'Mouse', 2),
(101, 10, '2026-02-03', 1, 'Ada Lovelace', '[email protected]', 'Keyboard', 1);Ada's name and email appear three times. The product name "Keyboard" appears twice. Storing the same fact more than once is called redundancy. Redundancy costs storage, but the bigger cost is that the copies can disagree.
The three anomalies
An anomaly is a problem that comes from the table's structure, not from a bug in your queries. There are three kinds.
| Anomaly | What goes wrong | Example in `order_lines_flat` |
|---|---|---|
| Update | A fact changes, but only some copies are updated | Ada's email changes on one row and stays old on the others |
| Insertion | A fact cannot be stored without an unrelated fact | You cannot add a new product until someone orders it |
| Deletion | Deleting one fact destroys another | Deleting the only line for "Mouse" erases the product |
The update anomaly is the easiest to trigger. This statement looks correct but is incomplete:
UPDATE order_lines_flat
SET customer_email = '[email protected]'
WHERE order_id = 100;Order 101 still holds [email protected]. The table now says Ada has two email addresses. No constraint can catch this, because both values are individually valid.
The insertion anomaly follows from the primary key. A product row needs an order_id because the key is (order_id, product_id), and NOT NULL forbids leaving it empty. The deletion anomaly is the mirror image. If order 100 is the only order containing a mouse, deleting that line also deletes everything you knew about mice.
Functional dependencies: the idea behind the rules
Normal forms are all defined using functional dependencies. A functional dependency A → B means that if you know the value of A, exactly one value of B is possible. Read it as "A determines B".
In order_lines_flat, these dependencies hold:
order_id → order_date, customer_idcustomer_id → customer_name, customer_emailproduct_id → product_name(order_id, product_id) → quantity
Each anomaly appears when a column depends on something other than the whole primary key. Only quantity depends on the full key (order_id, product_id). Every other column depends on part of the key, or on a non-key column.
The normal forms
A normal form is a checklist for a table's structure. Each form includes the previous one.
First normal form (1NF)
Every column holds a single value per row, and there are no repeating groups. A column like products = 'Keyboard, Mouse' violates 1NF. You cannot filter, join, or constrain the individual items. The fix is one row per item, which is what order_items does later in this chapter.
Second normal form (2NF)
The table is in 1NF, and every non-key column depends on the whole primary key. This only matters when the key has more than one column. In order_lines_flat, product_name depends only on product_id, which is half of the key. That is a partial dependency. order_date depends only on order_id, so it is also partial.
The fix is to move each partially dependent column into a table keyed by the part it depends on. Product facts go in products. Order facts go in orders.
Third normal form (3NF)
The table is in 2NF, and no non-key column depends on another non-key column. In the order facts, customer_name depends on customer_id, which is not the key of the order. That is a transitive dependency: order_id → customer_id → customer_name. The fix is to move customer facts into customers.
A common summary of 3NF is that every non-key column depends on the key, the whole key, and nothing but the key.
Decomposing the example
Applying those fixes gives four tables. In a fresh database, create them and fill them from the flat table.
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE
);
CREATE TABLE products (
product_id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL
);
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL REFERENCES customers (customer_id),
order_date DATE NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER NOT NULL REFERENCES orders (order_id),
product_id INTEGER NOT NULL REFERENCES products (product_id),
quantity INTEGER NOT NULL CHECK (quantity > 0),
PRIMARY KEY (order_id, product_id)
);
INSERT INTO customers (customer_id, name, email)
SELECT DISTINCT customer_id, customer_name, customer_email
FROM order_lines_flat;
INSERT INTO products (product_id, name)
SELECT DISTINCT product_id, product_name
FROM order_lines_flat;
INSERT INTO orders (order_id, customer_id, order_date)
SELECT DISTINCT order_id, customer_id, order_date
FROM order_lines_flat;
INSERT INTO order_items (order_id, product_id, quantity)
SELECT order_id, product_id, quantity
FROM order_lines_flat;The DISTINCT keyword collapses the repeated copies into one row per fact. The order of the inserts matters, because foreign keys require the referenced rows to exist first.
Now the anomalies are gone:
UPDATE customers
SET email = '[email protected]'
WHERE customer_id = 1;
INSERT INTO products (product_id, name) VALUES (12, 'Monitor');
DELETE FROM order_items WHERE order_id = 100 AND product_id = 11;The email change touches one row. A product can exist before anyone orders it. Deleting an order line leaves the product in products. To read the original wide view again, join the tables:
SELECT o.order_id, o.order_date, c.name AS customer_name,
p.name AS product_name, oi.quantity
FROM order_items AS oi
JOIN orders AS o ON o.order_id = oi.order_id
JOIN customers AS c ON c.customer_id = o.customer_id
JOIN products AS p ON p.product_id = oi.product_id
ORDER BY o.order_id, p.name;Common errors
- Normalizing by feel. Splitting tables because a table "looks big" is not normalization. Start from the dependencies. Ask what determines what.
- Forgetting the foreign keys. If
orders.customer_idhas noREFERENCESclause, the split tables can drift apart. An order could point to a customer that does not exist. - Using a changeable value as the key. Email is a poor primary key because people change it, and every referencing row would have to change too. A surrogate key such as
customer_idstays stable. KeepUNIQUEon the email so duplicates are still rejected. - Storing derived values. A column like
orders.total_amountthat can be computed fromorder_itemsis a redundant copy. You can store it, but you then own the job of keeping it in sync. - Stopping at 1NF. A table with single-valued columns can still have every anomaly in this chapter.
When to denormalize
Denormalization means deliberately reintroducing redundancy. The usual reasons are read speed and simpler queries. Reporting tables, data warehouses, and cached summary columns often do this. The trade-off is that you now carry the update anomaly risk again. Write the rule that keeps the copies in sync, such as a transaction that updates both places, or a periodic rebuild job.
A sound approach is to normalize first, measure real query performance, and denormalize only where a measured problem exists. An index, covered in a later chapter, often solves a join-speed problem without giving up the clean design.
The normal forms continue past 3NF, including Boyce-Codd normal form and 4NF. For most application databases, 3NF is the practical target.
Exercise
A table employees(employee_id PRIMARY KEY, name, department_id, department_name) has the dependency department_id → department_name. Which normal form does it violate, and how do you fix it?
Show answer
It violates third normal form, because department_name depends on department_id, which is a non-key column (a transitive dependency). Create departments(department_id PRIMARY KEY, department_name), remove department_name from employees, and make employees.department_id a foreign key referencing departments.
Exercise
In order_lines_flat, a customer who has never ordered anything cannot be recorded. Which anomaly is this, and which table in the normalized design removes it?
Show answer
It is an insertion anomaly. The customers table removes it, because a customer row can exist with no matching rows in orders.
Exercise
After the decomposition, you run DELETE FROM customers WHERE customer_id = 1; while orders for that customer exist. What happens, and why?
Show answer
The database rejects the statement with a foreign key violation (under the default behavior). orders.customer_id references customers, so deleting a customer that orders still point to would leave orphaned orders. You must delete or reassign the orders first.