Database Normalization · Lesson 40 of 112

Database Normalizing a Real Table

What you will learn

  • A repeatable method for normalizing a table
  • How to move data into new tables with INSERT ... SELECT DISTINCT
  • How to prove the split lost nothing
  • How the new design removes the anomalies

1. The method

  1. Write down the real-world functional dependencies (lesson 5.2).
  2. Find the key of the flat table.
  3. Remove lists and repeating groups (1NF).
  4. Remove partial dependencies (2NF).
  5. Remove transitive dependencies (3NF).
  6. Create the tables, load them, and verify by rebuilding the original with joins.

2. The flat table

A shop exports everything into one table:

order_id order_date customer customer_city product category unit_price qty
1 2026-03-01 Rina Dhaka Pen Stationery 10 3
1 2026-03-01 Rina Dhaka Book Stationery 120 1
2 2026-03-02 Sam Khulna Pen Stationery 10 10
3 2026-03-02 Rina Dhaka Bag Travel 500 1
4 2026-03-05 Lee Dhaka Book Stationery 120 2

This lesson runs on the sample database you chose in lesson 4.1 (use the Example data picker at the top). It adds small tables of its own to your browser's copy; run the examples from top to bottom, and press Reset data to start again. The flat table is called sales; the new tables get the prefix n_ (for "normalized") so they cannot clash with the real tables.

CREATE TABLE sales (
    order_id INTEGER, order_date TEXT, customer TEXT, customer_city TEXT,
    product TEXT, category TEXT, unit_price INTEGER, qty INTEGER
);
INSERT INTO sales VALUES
    (1, '2026-03-01', 'Rina', 'Dhaka', 'Pen', 'Stationery', 10, 3),
    (1, '2026-03-01', 'Rina', 'Dhaka', 'Book', 'Stationery', 120, 1),
    (2, '2026-03-02', 'Sam', 'Khulna', 'Pen', 'Stationery', 10, 10),
    (3, '2026-03-02', 'Rina', 'Dhaka', 'Bag', 'Travel', 500, 1),
    (4, '2026-03-05', 'Lee', 'Dhaka', 'Book', 'Stationery', 120, 2);
SELECT * FROM sales ORDER BY order_id, product;
order_id  order_date  customer  customer_city  product  category    unit_price  qty
--------  ----------  --------  -------------  -------  ----------  ----------  ---
       1  2026-03-01  Rina      Dhaka          Book     Stationery         120    1
       1  2026-03-01  Rina      Dhaka          Pen      Stationery          10    3
       2  2026-03-02  Sam       Khulna         Pen      Stationery          10   10
       3  2026-03-02  Rina      Dhaka          Bag      Travel             500    1
       4  2026-03-05  Lee       Dhaka          Book     Stationery         120    2
(5 rows)
CREATE TABLE sales (
    order_id INTEGER, order_date TEXT, customer TEXT, customer_city TEXT,
    product TEXT, category TEXT, unit_price INTEGER, qty INTEGER
);
INSERT INTO sales VALUES
    (1, '2026-03-01', 'Rina', 'Dhaka', 'Pen', 'Stationery', 10, 3),
    (1, '2026-03-01', 'Rina', 'Dhaka', 'Book', 'Stationery', 120, 1),
    (2, '2026-03-02', 'Sam', 'Khulna', 'Pen', 'Stationery', 10, 10),
    (3, '2026-03-02', 'Rina', 'Dhaka', 'Bag', 'Travel', 500, 1),
    (4, '2026-03-05', 'Lee', 'Dhaka', 'Book', 'Stationery', 120, 2);
SELECT * FROM sales ORDER BY order_id, product;
order_id  order_date  customer  customer_city  product  category    unit_price  qty
--------  ----------  --------  -------------  -------  ----------  ----------  ---
       1  2026-03-01  Rina      Dhaka          Book     Stationery         120    1
       1  2026-03-01  Rina      Dhaka          Pen      Stationery          10    3
       2  2026-03-02  Sam       Khulna         Pen      Stationery          10   10
       3  2026-03-02  Rina      Dhaka          Bag      Travel             500    1
       4  2026-03-05  Lee       Dhaka          Book     Stationery         120    2
(5 rows)
CREATE TABLE sales (
    order_id INTEGER, order_date TEXT, customer TEXT, customer_city TEXT,
    product TEXT, category TEXT, unit_price INTEGER, qty INTEGER
);
INSERT INTO sales VALUES
    (1, '2026-03-01', 'Rina', 'Dhaka', 'Pen', 'Stationery', 10, 3),
    (1, '2026-03-01', 'Rina', 'Dhaka', 'Book', 'Stationery', 120, 1),
    (2, '2026-03-02', 'Sam', 'Khulna', 'Pen', 'Stationery', 10, 10),
    (3, '2026-03-02', 'Rina', 'Dhaka', 'Bag', 'Travel', 500, 1),
    (4, '2026-03-05', 'Lee', 'Dhaka', 'Book', 'Stationery', 120, 2);
SELECT * FROM sales ORDER BY order_id, product;
order_id  order_date  customer  customer_city  product  category    unit_price  qty
--------  ----------  --------  -------------  -------  ----------  ----------  ---
       1  2026-03-01  Rina      Dhaka          Book     Stationery         120    1
       1  2026-03-01  Rina      Dhaka          Pen      Stationery          10    3
       2  2026-03-02  Sam       Khulna         Pen      Stationery          10   10
       3  2026-03-02  Rina      Dhaka          Bag      Travel             500    1
       4  2026-03-05  Lee       Dhaka          Book     Stationery         120    2
(5 rows)

3. Step 1: the dependencies

Real-world rules for this shop (assumed for this example: customer names are unique, product names are unique, and the price list is the current price):

  • order_id -> order_date, customer: an order has one date and one customer.
  • customer -> customer_city.
  • product -> category, unit_price.
  • (order_id, product) -> qty.

The key of the flat table is (order_id, product).

4. Steps 2 to 5: find the problems

Dependency Problem Form
order_id -> order_date, customer depends on part of the key 2NF
product -> category, unit_price depends on part of the key 2NF
customer -> customer_city depends on a non-key (customer) 3NF

The result is four tables, each about one thing: customers, products, orders, order_items.

5. Step 6: create and load

Use surrogate keys (lesson 2.5) so joins are short, then fill the new tables from the flat table:

CREATE TABLE n_customers (customer_id INTEGER PRIMARY KEY, name TEXT UNIQUE NOT NULL, city TEXT NOT NULL);
CREATE TABLE n_products (product_id INTEGER PRIMARY KEY, name TEXT UNIQUE NOT NULL, category TEXT NOT NULL, unit_price INTEGER NOT NULL);
CREATE TABLE n_orders (
    order_id    INTEGER PRIMARY KEY,
    order_date  TEXT NOT NULL,
    customer_id INTEGER NOT NULL REFERENCES n_customers (customer_id)
);
CREATE TABLE n_order_items (
    order_id   INTEGER REFERENCES n_orders (order_id),
    product_id INTEGER REFERENCES n_products (product_id),
    qty        INTEGER NOT NULL,
    PRIMARY KEY (order_id, product_id)
);

INSERT INTO n_customers (name, city) SELECT DISTINCT customer, customer_city FROM sales ORDER BY customer;
INSERT INTO n_products (name, category, unit_price) SELECT DISTINCT product, category, unit_price FROM sales ORDER BY product;
INSERT INTO n_orders SELECT DISTINCT s.order_id, s.order_date, c.customer_id FROM sales s JOIN n_customers c ON c.name = s.customer;
INSERT INTO n_order_items SELECT s.order_id, p.product_id, s.qty FROM sales s JOIN n_products p ON p.name = s.product;
(5 rows changed)
SELECT * FROM n_customers ORDER BY 1;
SELECT * FROM n_products ORDER BY 1;
SELECT * FROM n_orders ORDER BY 1;
SELECT * FROM n_order_items ORDER BY 1, 2;
customer_id  name  city
-----------  ----  ------
          1  Lee   Dhaka
          2  Rina  Dhaka
          3  Sam   Khulna
(3 rows)

product_id  name  category    unit_price
----------  ----  ----------  ----------
         1  Bag   Travel             500
         2  Book  Stationery         120
         3  Pen   Stationery          10
(3 rows)

order_id  order_date  customer_id
--------  ----------  -----------
       1  2026-03-01            2
       2  2026-03-02            3
       3  2026-03-02            2
       4  2026-03-05            1
(4 rows)

order_id  product_id  qty
--------  ----------  ---
       1           2    1
       1           3    3
       2           3   10
       3           1    1
       4           2    2
(5 rows)
CREATE TABLE n_customers (customer_id INTEGER PRIMARY KEY, name TEXT UNIQUE NOT NULL, city TEXT NOT NULL);
CREATE TABLE n_products (product_id INTEGER PRIMARY KEY, name TEXT UNIQUE NOT NULL, category TEXT NOT NULL, unit_price INTEGER NOT NULL);
CREATE TABLE n_orders (
    order_id    INTEGER PRIMARY KEY,
    order_date  TEXT NOT NULL,
    customer_id INTEGER NOT NULL REFERENCES n_customers (customer_id)
);
CREATE TABLE n_order_items (
    order_id   INTEGER REFERENCES n_orders (order_id),
    product_id INTEGER REFERENCES n_products (product_id),
    qty        INTEGER NOT NULL,
    PRIMARY KEY (order_id, product_id)
);

INSERT INTO n_customers (name, city) SELECT DISTINCT customer, customer_city FROM sales ORDER BY customer;
INSERT INTO n_products (name, category, unit_price) SELECT DISTINCT product, category, unit_price FROM sales ORDER BY product;
INSERT INTO n_orders SELECT DISTINCT s.order_id, s.order_date, c.customer_id FROM sales s JOIN n_customers c ON c.name = s.customer;
INSERT INTO n_order_items SELECT s.order_id, p.product_id, s.qty FROM sales s JOIN n_products p ON p.name = s.product;
(5 rows changed)
SELECT * FROM n_customers ORDER BY 1;
SELECT * FROM n_products ORDER BY 1;
SELECT * FROM n_orders ORDER BY 1;
SELECT * FROM n_order_items ORDER BY 1, 2;
customer_id  name  city
-----------  ----  ------
          1  Lee   Dhaka
          2  Rina  Dhaka
          3  Sam   Khulna
(3 rows)

product_id  name  category    unit_price
----------  ----  ----------  ----------
         1  Bag   Travel             500
         2  Book  Stationery         120
         3  Pen   Stationery          10
(3 rows)

order_id  order_date  customer_id
--------  ----------  -----------
       1  2026-03-01            2
       2  2026-03-02            3
       3  2026-03-02            2
       4  2026-03-05            1
(4 rows)

order_id  product_id  qty
--------  ----------  ---
       1           2    1
       1           3    3
       2           3   10
       3           1    1
       4           2    2
(5 rows)
CREATE TABLE n_customers (customer_id INTEGER PRIMARY KEY, name TEXT UNIQUE NOT NULL, city TEXT NOT NULL);
CREATE TABLE n_products (product_id INTEGER PRIMARY KEY, name TEXT UNIQUE NOT NULL, category TEXT NOT NULL, unit_price INTEGER NOT NULL);
CREATE TABLE n_orders (
    order_id    INTEGER PRIMARY KEY,
    order_date  TEXT NOT NULL,
    customer_id INTEGER NOT NULL REFERENCES n_customers (customer_id)
);
CREATE TABLE n_order_items (
    order_id   INTEGER REFERENCES n_orders (order_id),
    product_id INTEGER REFERENCES n_products (product_id),
    qty        INTEGER NOT NULL,
    PRIMARY KEY (order_id, product_id)
);

INSERT INTO n_customers (name, city) SELECT DISTINCT customer, customer_city FROM sales ORDER BY customer;
INSERT INTO n_products (name, category, unit_price) SELECT DISTINCT product, category, unit_price FROM sales ORDER BY product;
INSERT INTO n_orders SELECT DISTINCT s.order_id, s.order_date, c.customer_id FROM sales s JOIN n_customers c ON c.name = s.customer;
INSERT INTO n_order_items SELECT s.order_id, p.product_id, s.qty FROM sales s JOIN n_products p ON p.name = s.product;
(5 rows changed)
SELECT * FROM n_customers ORDER BY 1;
SELECT * FROM n_products ORDER BY 1;
SELECT * FROM n_orders ORDER BY 1;
SELECT * FROM n_order_items ORDER BY 1, 2;
customer_id  name  city
-----------  ----  ------
          1  Lee   Dhaka
          2  Rina  Dhaka
          3  Sam   Khulna
(3 rows)

product_id  name  category    unit_price
----------  ----  ----------  ----------
         1  Bag   Travel             500
         2  Book  Stationery         120
         3  Pen   Stationery          10
(3 rows)

order_id  order_date  customer_id
--------  ----------  -----------
       1  2026-03-01            2
       2  2026-03-02            3
       3  2026-03-02            2
       4  2026-03-05            1
(4 rows)

order_id  product_id  qty
--------  ----------  ---
       1           2    1
       1           3    3
       2           3   10
       3           1    1
       4           2    2
(5 rows)

Five repeated lines have become small, clean tables. INSERT ... SELECT DISTINCT copied each fact exactly once. If DISTINCT had produced two rows for one customer name (say two different cities), UNIQUE on name would have refused the load. That is a useful alarm: it means the supposed rule customer -> customer_city is false in your data.

6. Prove nothing was lost

Rebuild the flat table with joins and compare in both directions. EXCEPT returns rows in the first query that are missing from the second; zero both ways means the two are identical:

CREATE VIEW rebuilt AS
SELECT o.order_id, o.order_date, c.name AS customer, c.city AS customer_city,
       p.name AS product, p.category, p.unit_price, i.qty
FROM n_order_items i
JOIN n_orders o USING (order_id)
JOIN n_customers c USING (customer_id)
JOIN n_products p USING (product_id);
SELECT COUNT(*) AS in_sales_not_rebuilt FROM (SELECT * FROM sales EXCEPT SELECT * FROM rebuilt);
SELECT COUNT(*) AS in_rebuilt_not_sales FROM (SELECT * FROM rebuilt EXCEPT SELECT * FROM sales);
in_sales_not_rebuilt
--------------------
                   0
(1 row)

in_rebuilt_not_sales
--------------------
                   0
(1 row)
CREATE VIEW rebuilt AS
SELECT o.order_id, o.order_date, c.name AS customer, c.city AS customer_city,
       p.name AS product, p.category, p.unit_price, i.qty
FROM n_order_items i
JOIN n_orders o USING (order_id)
JOIN n_customers c USING (customer_id)
JOIN n_products p USING (product_id);
SELECT COUNT(*) AS in_sales_not_rebuilt FROM (SELECT * FROM sales EXCEPT SELECT * FROM rebuilt);
SELECT COUNT(*) AS in_rebuilt_not_sales FROM (SELECT * FROM rebuilt EXCEPT SELECT * FROM sales);
in_sales_not_rebuilt
--------------------
                   0
(1 row)

in_rebuilt_not_sales
--------------------
                   0
(1 row)
CREATE VIEW rebuilt AS
SELECT o.order_id, o.order_date, c.name AS customer, c.city AS customer_city,
       p.name AS product, p.category, p.unit_price, i.qty
FROM n_order_items i
JOIN n_orders o USING (order_id)
JOIN n_customers c USING (customer_id)
JOIN n_products p USING (product_id);
SELECT COUNT(*) AS in_sales_not_rebuilt FROM (SELECT * FROM sales EXCEPT SELECT * FROM rebuilt);
SELECT COUNT(*) AS in_rebuilt_not_sales FROM (SELECT * FROM rebuilt EXCEPT SELECT * FROM sales);
in_sales_not_rebuilt
--------------------
                   0
(1 row)

in_rebuilt_not_sales
--------------------
                   0
(1 row)

The split is lossless: the joins recreate every original row and invent none.

7. The anomalies are gone

UPDATE n_customers SET city = 'Chattogram' WHERE name = 'Rina';
SELECT DISTINCT customer, customer_city FROM rebuilt WHERE customer = 'Rina';
customer  customer_city
--------  -------------
Rina      Chattogram
(1 row)
INSERT INTO n_customers (name, city) VALUES ('Amy', 'Sylhet');
SELECT c.name, COUNT(o.order_id) AS orders
FROM n_customers c LEFT JOIN n_orders o USING (customer_id)
GROUP BY c.name
ORDER BY c.name;
name  orders
----  ------
Amy        0
Lee        1
Rina       2
Sam        1
(4 rows)
UPDATE n_customers SET city = 'Chattogram' WHERE name = 'Rina';
SELECT DISTINCT customer, customer_city FROM rebuilt WHERE customer = 'Rina';
customer  customer_city
--------  -------------
Rina      Chattogram
(1 row)
INSERT INTO n_customers (name, city) VALUES ('Amy', 'Sylhet');
SELECT c.name, COUNT(o.order_id) AS orders
FROM n_customers c LEFT JOIN n_orders o USING (customer_id)
GROUP BY c.name
ORDER BY c.name;
name  orders
----  ------
Amy        0
Lee        1
Rina       2
Sam        1
(4 rows)
UPDATE n_customers SET city = 'Chattogram' WHERE name = 'Rina';
SELECT DISTINCT customer, customer_city FROM rebuilt WHERE customer = 'Rina';
customer  customer_city
--------  -------------
Rina      Chattogram
(1 row)
INSERT INTO n_customers (name, city) VALUES ('Amy', 'Sylhet');
SELECT c.name, COUNT(o.order_id) AS orders
FROM n_customers c LEFT JOIN n_orders o USING (customer_id)
GROUP BY c.name
ORDER BY c.name;
name  orders
----  ------
Amy        0
Lee        1
Rina       2
Sam        1
(4 rows)
  • One UPDATE changed Rina's city on all her orders (update anomaly gone).
  • Amy exists with zero orders (insert anomaly gone).

Worked example: your turn on a small table

loans (loan_id, member_id, member_name, book_id, title, loan_date), key loan_id.

  1. FDs: loan_id -> member_id, book_id, loan_date; member_id -> member_name; book_id -> title.
  2. 2NF: the key is a single column, so no partial dependencies.
  3. 3NF: member_id -> member_name and book_id -> title have non-key left sides.
  4. Result: members (member_id, member_name), books (book_id, title), loans (loan_id, member_id, book_id, loan_date).
  5. Verify by joining and comparing with the original using EXCEPT both ways. Data is made up.

Common mistakes

  • Normalizing from the sample data instead of the real rules. Confirm each dependency is a business rule (lesson 5.2).
  • Loading the new tables but never comparing with the original. Always run the EXCEPT check.
  • Using a text column as the join key when names can repeat. Here names are unique by assumption; in real life use IDs the customer cannot share.
  • Dropping the flat table before the new tables are verified.

Key takeaways

  • Method: list dependencies, find the key, fix 1NF, 2NF, 3NF, then create, load and verify.
  • INSERT ... SELECT DISTINCT moves each fact into its own table once.
  • UNIQUE constraints turn a wrong assumed rule into an error at load time.
  • A lossless split rebuilds the original exactly; prove it with EXCEPT both ways.
  • The result removes the update, insert and delete anomalies.

Practice

1. What is the key of the flat sales table?

Show answer

(order_id, product): one line per product per order.

2. Which dependency in the sales table is transitive?

Show answer

customer -> customer_city: the city depends on the customer, which is not the key. (Through the order it is order_id -> customer -> customer_city.)

3. What does INSERT ... SELECT DISTINCT do for the customers table?

Show answer

It copies each different customer name and city pair from the flat table once.

4. How do you prove the split lost no data?

Show answer

Rebuild the flat table with joins and check that EXCEPT returns no rows in either direction.

5. What would happen at load time if Rina appeared with two different cities?

Show answer

The DISTINCT result would hold two Rina rows and the UNIQUE constraint on name would refuse the second, revealing that the assumed rule was false.

6. Normalize staff (staff_id, name, branch_id, branch_city), key staff_id.

Show answer

branches (branch_id, branch_city) and staff (staff_id, name, branch_id).