Chapter 9 · Lesson 2 of 9
Feel the Update Anomaly
Experience an update anomaly in a flat orders table, where one customer's city is repeated on many rows that would all need changing.
Let's make the update problem concrete. In the bad schema below, customer details are copied into every order row.
If Alice moves from Berlin to Munich, you do not update one customer record. You must find every row where Alice appears and change all of them. Missing just one row leaves the database in an inconsistent state.
Load the sample data and query the rows that would need to be changed. You should notice that one real-world fact — Alice's customer_city — is duplicated across multiple order rows.
Sample data
This lesson loads the orders_flat table (denormalized order lines) into DuckDB in your browser with one click, so you can run every example without uploading anything.
Setup SQL for orders_flat
CREATE OR REPLACE TABLE orders_flat (
order_id INTEGER,
customer_name VARCHAR,
customer_email VARCHAR,
customer_city VARCHAR,
product_name VARCHAR,
product_price DECIMAL(10,2),
quantity INTEGER
);
INSERT INTO orders_flat VALUES
(1001, 'Alice Kim', '[email protected]', 'Berlin', 'Keyboard', 89.00, 1),
(1002, 'Alice Kim', '[email protected]', 'Berlin', 'Mouse', 25.00, 2),
(1003, 'Ben Fox', '[email protected]', 'Hamburg', 'Keyboard', 89.00, 1),
(1004, 'Alice Kim', '[email protected]', 'Berlin', 'USB-C Hub', 45.00, 1),
(1005, 'Cara Lane', '[email protected]', 'Munich', 'Webcam', 120.00, 1),
(1006, 'Dan Reed', '[email protected]', 'Cologne', 'Desk Mat', 19.00, 1);Practice challenge
Return the rows that would need updating if Alice Kim changed cities. Show order_id, customer_name, and customer_city, ordered by order_id.
Hint: Query orders_flat, filter to Alice Kim, select the three requested columns, and order by order_id.
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.