Chapter 9 · Lesson 3 of 9
One Row to Update in a Good Schema
See how a normalized customers table turns a risky multi-row update into a change to a single row, then query that one row.
Now compare that with a normalized design. Customer facts live in the customers table, while orders only stores customer_id.
If Alice moves, you update her row once in customers. Her orders do not need their own copies of city and email.
This is one of the main reasons normalized tables are easier to maintain correctly.
Sample data
This lesson loads the customers, products, orders, and order_items tables into DuckDB in your browser with one click, so you can run every example without uploading anything.
Setup SQL for customers, products, orders, order_items
CREATE OR REPLACE TABLE customers (
customer_id INTEGER,
customer_name VARCHAR,
customer_email VARCHAR,
customer_city VARCHAR
);
INSERT INTO customers VALUES
(1, 'Alice Kim', '[email protected]', 'Berlin'),
(2, 'Ben Fox', '[email protected]', 'Hamburg'),
(3, 'Cara Lane', '[email protected]', 'Munich'),
(4, 'Dan Reed', '[email protected]', 'Cologne');
CREATE OR REPLACE TABLE products (
product_id INTEGER,
product_name VARCHAR,
product_price DECIMAL(10,2)
);
INSERT INTO products VALUES
(1, 'Keyboard', 89.00),
(2, 'Mouse', 25.00),
(3, 'USB-C Hub', 45.00),
(4, 'Webcam', 120.00),
(5, 'Desk Mat', 19.00),
(6, 'Monitor Stand', 59.00);
CREATE OR REPLACE TABLE orders (
order_id INTEGER,
customer_id INTEGER,
order_date DATE
);
INSERT INTO orders VALUES
(1001, 1, '2024-01-05'),
(1002, 1, '2024-01-07'),
(1003, 2, '2024-01-09'),
(1004, 1, '2024-01-10'),
(1005, 3, '2024-01-14'),
(1006, 4, '2024-01-15');
CREATE OR REPLACE TABLE order_items (
order_id INTEGER,
product_id INTEGER,
quantity INTEGER
);
INSERT INTO order_items VALUES
(1001, 1, 1),
(1002, 2, 2),
(1003, 1, 1),
(1004, 3, 1),
(1005, 4, 1),
(1006, 5, 1);Practice challenge
Return the single customer row you would update for Alice Kim. Show customer_id, customer_name, and customer_city.
Hint: Query the customers table and filter to Alice Kim.
Run your query in the editor and the lesson checks the result automatically.
Lessons in Normalization
See why well-designed databases split data into related tables, feel update, insert, and delete anomalies, and learn 1NF, 2NF, and 3NF.