Understanding Database Normalization in SQL with Example
See how raw, repetitive data gets organized into clean, related tables.
Normalization is the process of structuring a database to reduce redundancy and prevent data inconsistencies. Rather than explaining it in the abstract, it's easiest to understand through an example table that gets progressively cleaned up.
The Unnormalized Starting Point
OrderID | Customer | Product1 | Product2 1 | Riya | Pen | Notebook 2 | Aman | Pen | NULL
Storing multiple products in separate columns makes it hard to add a third product, and wastes space with NULLs.
First Normal Form (1NF)
Eliminate repeating groups — each row should hold a single value per column.
OrderID | Customer | Product 1 | Riya | Pen 1 | Riya | Notebook 2 | Aman | Pen
Second Normal Form (2NF)
Remove partial dependency — every non-key column must depend on the whole primary key, not just part of it. This usually means splitting into separate Orders and OrderItems tables.
Orders: OrderID, Customer OrderItems: OrderID, Product
Third Normal Form (3NF)
Remove transitive dependency — non-key columns shouldn't depend on other non-key columns. If Customer also stored a CustomerCity, that belongs in its own Customers table instead.
Customers: CustomerID, Name, City Orders: OrderID, CustomerID OrderItems: OrderID, Product
Why Normalize?
- Prevents the same fact from being stored (and potentially updated inconsistently) in multiple places
- Makes updates and deletes safer and more predictable
- Keeps tables focused on a single, clear responsibility
The Problems Normalization Prevents
Unnormalized tables suffer from three kinds of anomalies. Seeing them with the order example makes the purpose of each normal form clearer.
- Update anomaly: if Riya's city is stored on every order row, changing it means updating many rows. Miss one and the data now disagrees with itself.
- Insert anomaly: you cannot record a new customer until they place an order, because customer details only live in the orders table.
- Delete anomaly: deleting Aman's only order also deletes the only record that Aman exists.
Building the 3NF Design in SQL
The final design from the example becomes three tables linked by keys. Foreign keys make the database itself refuse orders for customers who do not exist.
CREATE TABLE customers (
customer_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
city VARCHAR(60)
);
CREATE TABLE orders (
order_id INT AUTO_INCREMENT PRIMARY KEY,
customer_id INT NOT NULL,
order_date DATE NOT NULL,
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);
CREATE TABLE order_items (
order_id INT NOT NULL,
product VARCHAR(100) NOT NULL,
quantity INT NOT NULL DEFAULT 1,
PRIMARY KEY (order_id, product),
FOREIGN KEY (order_id) REFERENCES orders(order_id)
);
In a fuller design, product would also move into a products table with its own ID and price, and order_items would store the product ID.
Getting the Combined View Back
Normalized data is joined at query time. This reproduces the original wide view without storing anything twice.
SELECT o.order_id, c.name, c.city, oi.product, oi.quantity
FROM orders o
JOIN customers c ON c.customer_id = o.customer_id
JOIN order_items oi ON oi.order_id = o.order_id
ORDER BY o.order_id;
Index the foreign key columns so these joins stay fast as the tables grow. InnoDB creates an index on a foreign key column automatically if one does not already exist.
A Quick Way to Check Each Form
| Form | Question to ask |
|---|---|
| 1NF | Does every cell hold one value, with no repeating columns like Product1, Product2? |
| 2NF | With a composite key, does every other column need the whole key? |
| 3NF | Does any non-key column depend on another non-key column? |
| BCNF | Is every column that determines another column a candidate key? |
When to Denormalize on Purpose
Reporting databases and dashboards often copy data into wide tables so queries avoid many joins. That is a deliberate trade: reads get faster, but updates must keep copies in sync. A good habit is to keep the main transactional database normalized and build denormalized summary tables or a warehouse from it.
One more common exception: an order should store the price at the time of purchase, even though the product table has a current price. That is not duplication, because the two values mean different things.
Before denormalizing, measure. Many slow join queries become fast with the right index, which keeps the clean design and solves the real problem.