# Normalization

> Store each fact once, with three normal forms in plain words, and see the update, insertion and deletion problems it prevents.

Subject: SQL | Lesson 37 of 44 | 7 min read
Interactive lesson: https://codewithanimation.in/sql/normalization
Previous: [Constraints & Defaults](https://codewithanimation.in/sql/constraints-and-defaults.md) | Next: [Views](https://codewithanimation.in/sql/views.md)

## Each fact in one place

**Normalization** is a way of designing tables so that each fact is stored once, in the table where it belongs. It was introduced by Edgar Codd in 1970, with the aim of removing problems when rows are inserted, updated and deleted.

It is done in steps called **normal forms**. The first three are the ones you will use every day: 1NF, 2NF and 3NF.

## Stored twice means wrong once

If Asha's city is written in every one of her orders, then moving house means finding and changing every row. Miss one, and the database now disagrees with itself. Storing a fact once removes that risk.

## The three forms, in plain words

**1NF: one value per field.** A field holds a single value, not a list such as `'Pen, Ruler'`. Give each item its own row.

**2NF: the whole key.** When a row is identified by two columns together, every other column must depend on both, not on just one of them. A product's name belongs in a products table, not in every order line.

**3NF: nothing but the key.** A column must depend on the key, not on another non-key column. If an order stores `customer` and also the customer's `city`, the city depends on the customer, not on the order, so it moves to a `customers` table.

The example splits one wide table, `orders_flat`, into `customers` and `orders`. The orders point at the customer, and each city is stored once.

## Common mistake: Three anomalies, and when to stop

In the wide table, an **update anomaly**: changing one row leaves Asha with two cities. An **insertion anomaly**: you cannot add a customer who has not ordered, because a primary key cannot be NULL. A **deletion anomaly**: deleting Ben's only order erases everything you knew about Ben. The split tables have none of these.

Normalizing has a cost: you need joins to put the facts back together. Reporting tables, caches and search indexes are sometimes **denormalized** on purpose, to read faster. That is a choice you make after you have a correct design, never instead of one.

Do not over-split. A column such as a phone number that belongs to the person does not need its own table unless a person can have several.

## Tip: Ask: what does this column depend on?

For every column, say "it describes the ..." and fill in the blank. If the blank is not the thing identified by the key, that column belongs in another table.

## Try it: Break the wide table, then the split one

With One wide table, try each change and watch what goes wrong. Then switch to Split into two tables and try the same three.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/sql/normalization

## Check yourself

### Question 1

A table stores each order with the customer's city. A customer with 2 orders moves to Rome, and only one order row is updated. What is this problem called?

- A. An update anomaly
- B. An insertion anomaly
- C. A deletion anomaly

Correct answer: A

The same fact is stored in several rows, so a partial update leaves conflicting data.

- Why not B: An insertion anomaly is when you cannot add a fact because another fact is missing.
- Why not C: A deletion anomaly is when removing a row also removes unrelated facts.

### Question 2

A field holds the text 'Pen, Ruler, Tape'. Which normal form does that break?

- A. First normal form (1NF)
- B. Second normal form (2NF)
- C. Third normal form (3NF)

Correct answer: A

1NF requires each field to hold a single value. Give each item its own row.

- Why not B: 2NF is about columns depending on only part of a composite key.
- Why not C: 3NF is about one non-key column depending on another non-key column.

### Question 3

An orders table stores `customer` and `city`, and city depends on the customer. Which step fixes it?

- A. Move city into a customers table and point the orders at it (3NF)
- B. Add a UNIQUE rule on city
- C. Copy the city into every table

Correct answer: A

A column that depends on another non-key column belongs in the table of that other thing.

- Why not B: That would stop two customers from sharing a city, which is wrong, and does not remove the repeated fact.
- Why not C: That makes the problem bigger. Each fact should be stored once.

## Remember this

- Normalization means storing each fact once, in the table where it belongs.
- 1NF: one value per field. 2NF: depend on the whole key. 3NF: depend on nothing but the key.
- A badly designed table causes update, insertion and deletion anomalies.
- Normalizing costs joins. Denormalize only on purpose, for speed, after the design is correct.
