Sub topic 12
Normalization: avoiding redundancy and update anomalies
Assume you know what tables, rows, primary keys, foreign keys, and basic SELECT and JOIN queries are. This chapter uses those ideas to decide where each piece of data should live.
Why repeated data causes problems
Normalization is the process of organizing related data into tables so that each fact is stored in one appropriate place. The goal is to reduce redundancy, which means storing the same fact more times than necessary.
Suppose an order table stores customer details in every order row:
| order_id | customer_name | customer_email | product_name | quantity |
|---|---|---|---|---|
| 100 | Ada Lovelace | [email protected] | Notebook | 2 |
| 101 | Ada Lovelace | [email protected] | Pen | 3 |
The customer's name and email are repeated. Repetition creates update anomalies:
- An update anomaly occurs when one fact must be changed in several rows. If Ada changes her email and only one row is updated, the database contains conflicting values.
- An insertion anomaly occurs when a fact cannot be stored without adding an unrelated fact. For example, you may be unable to add a customer until that customer places an order.
- A deletion anomaly occurs when deleting one fact accidentally removes another. Deleting a customer's only order might also delete the only record of that customer.
Normalization separates facts according to what they describe.
A practical normalization pattern
A customer is a customer whether or not that customer has an order. An order is a separate fact, and an order item connects an order to a product. The schema can represent those facts directly:
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE
);
CREATE TABLE orders (
order_id INTEGER PRIMARY KEY,
customer_id INTEGER NOT NULL REFERENCES customers (customer_id),
order_date DATE NOT NULL
);
CREATE TABLE products (
product_id INTEGER PRIMARY KEY,
name VARCHAR(100) 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)
);Now the customer's email is stored once. Every order refers to the customer through customer_id. If the email changes, one row changes.
To display the combined information, use a join rather than copying values:
SELECT
o.order_id,
c.name AS customer_name,
p.name AS product_name,
oi.quantity
FROM orders AS o
JOIN customers AS c ON c.customer_id = o.customer_id
JOIN order_items AS oi ON oi.order_id = o.order_id
JOIN products AS p ON p.product_id = oi.product_id;Common normalization levels
Normalization is often described in levels called normal forms. Each level adds a rule.
| Normal form | Main rule | Typical violation |
|---|---|---|
| First normal form (1NF) | Each column contains one value, not a list | `phone_numbers` contains `"555-0100, 555-0101"` |
| Second normal form (2NF) | Non-key values depend on the whole key | A product name depends only on `product_id` in an order-item table |
| Third normal form (3NF) | Non-key values depend only on the key | A department name is stored in an employee row and really depends on `department_id` |
The order-items table follows the second-normal-form idea. Its key is (order_id, product_id). quantity depends on both values. Product details depend only on product_id, so they belong in products, not in order_items.
Choosing how far to normalize
Normalization is a design tool, not a rule that every database must follow perfectly. Highly normalized transactional systems are easier to update safely. Analytical systems may deliberately copy data into reporting tables so large reports need fewer joins.
When deciding, ask:
- What real-world fact does each column describe?
- Can the same fact appear in more than one row?
- What happens if that fact changes?
- Can a foreign key represent the relationship instead of copied text?
- Is denormalization solving a measured problem?
Exercise
In the original repeated-order design, where should a customer's email be stored, and how should an order refer to it?
Show answer
Store the email once in a customers table and give that customer a primary key. Store the customer's key as a foreign key in orders.