Database Repeated Data and Anomalies
What you will learn
- What redundancy (repeated data) is
- The three anomalies: update, insert and delete
- Why they happen
- The basic cure: one fact, one place
This lesson runs on the sample database you chose in lesson 4.1 (use the Example data picker at the top to change it). It changes your browser's copy of the data and the examples build on each other from top to bottom, so run them in order; Reset data starts again.
1. One big table
A beginner often puts everything into one wide table. We will build one on purpose from the real tables, with CREATE TABLE ... AS SELECT, which fills a new table with the rows of a query:
Everything about three customers' orders in one table: the customer, their phone and city, and what they bought.
CREATE TABLE orders_flat AS
SELECT oi.order_id, oi.line_no,
c.first_name || ' ' || c.last_name AS customer, c.phone, c.city,
p.name AS product, oi.unit_price AS price
FROM order_items oi
JOIN orders o ON o.order_id = oi.order_id
JOIN customers c ON c.customer_id = o.customer_id
JOIN products p ON p.product_id = oi.product_id
WHERE c.customer_id IN (3, 9, 93);
SELECT order_id, line_no, customer, phone, city, product, price FROM orders_flat ORDER BY order_id, line_no LIMIT 8;
order_id line_no customer phone city product price
-------- ------- ---------- ------------ ------ -------------- -----
451 1 Dana Costa +58 13864688 Cairo Water bottle 86.9
451 2 Dana Costa +58 13864688 Cairo Card game 17.05
451 3 Dana Costa +58 13864688 Cairo Foam roller 79.0
516 1 Lara Lee +90 73627612 Austin Storage box 24.0
955 1 Lara Lee +90 73627612 Austin Watering can 35.1
1039 1 Dana Costa +58 13864688 Cairo Pruning shears 29.5
1379 1 Dana Costa +58 13864688 Cairo Bookshelf 49.0
1389 1 Lara Lee +90 73627612 Austin Desk organiser 13.95
(8 rows)
Everything about the first fourteen accounts in one table: the customer, the account, and the branch with its city.
CREATE TABLE accounts_flat AS
SELECT a.account_id, c.full_name AS customer, a.account_type, b.name AS branch_name, b.city AS branch_city
FROM accounts a
JOIN customers c ON c.customer_id = a.customer_id
JOIN branches b ON b.branch_id = a.branch_id
WHERE a.account_id <= 14;
SELECT * FROM accounts_flat ORDER BY account_id;
account_id customer account_type branch_name branch_city
---------- ------------- ------------ ----------- -----------
1 Leo Kumar checking Central Lisbon
2 Hana Kim savings Riverside Toronto
3 Vera Banerjee savings Airport Lagos
4 Olga Garcia checking Central Lisbon
5 Amir Singh checking Airport Lagos
6 Rosa Meyer checking Airport Lagos
7 Ugo Ahmed savings Riverside Toronto
8 Ana Nguyen savings Old Town Cairo
9 Noah Tanaka savings Airport Lagos
10 Anil Rahman savings Riverside Toronto
11 Hana Torres checking Harbour Osaka
12 Dev Zhang checking Central Lisbon
... (14 rows in total, the first 12 shown)
Everything about the enrollments of the first four students in one table: the student, the course, the teacher and the teacher's email.
CREATE TABLE enrollment_flat AS
SELECT st.first_name || ' ' || st.last_name AS student, c.code AS course, t.name AS teacher, t.email AS teacher_email
FROM enrollments e
JOIN students st ON st.student_id = e.student_id
JOIN sections sec ON sec.section_id = e.section_id
JOIN courses c ON c.course_id = sec.course_id
JOIN teachers t ON t.teacher_id = sec.teacher_id
WHERE e.student_id <= 4;
SELECT * FROM enrollment_flat ORDER BY student, course;
student course teacher teacher_email
------------ ------- ------------ -----------------------------
Chloe Garcia ART101 Isla Mohamed isla.mohamed26@school.example
Chloe Garcia HUM101 Omar Ito omar.ito14@school.example
Chloe Garcia HUM201 Nico Diaz nico.diaz23@school.example
Chloe Garcia LANG101 Sven Okafor sven.okafor22@school.example
Elena Haque ART201 Hugo Hassan NULL
Elena Haque MATH201 Kira Perez kira.perez17@school.example
Elena Haque MATH301 Sven Silva sven.silva29@school.example
Elena Haque SCI201 Paulo Park NULL
Elena Haque SCI202 Omar Ito omar.ito14@school.example
Quinn Meyer MATH301 Nico Diaz nico.diaz23@school.example
Quinn Meyer SCI101 Dana Tanaka dana.tanaka25@school.example
Vera Khan ART101 Isla Mohamed isla.mohamed26@school.example
... (14 rows in total, the first 12 shown)
It works at first. But look at what is repeated:
SELECT customer, phone, city, COUNT(*) AS rows_in_table FROM orders_flat GROUP BY customer, phone, city ORDER BY customer;
customer phone city rows_in_table
------------ ------------ ------ -------------
Dana Costa +58 13864688 Cairo 12
Femi Bianchi +41 11471941 Lisbon 2
Lara Lee +90 73627612 Austin 6
(3 rows)
Each customer's phone and city appear once per order line.
SELECT branch_name, branch_city, COUNT(*) AS rows_in_table FROM accounts_flat GROUP BY branch_name, branch_city ORDER BY branch_name;
branch_name branch_city rows_in_table
----------- ----------- -------------
Airport Lagos 5
Central Lisbon 3
Harbour Osaka 1
Old Town Cairo 1
Riverside Toronto 3
University Pune 1
(6 rows)
Each branch's name and city appear once per account.
SELECT teacher, teacher_email, COUNT(*) AS rows_in_table FROM enrollment_flat GROUP BY teacher, teacher_email ORDER BY teacher;
teacher teacher_email rows_in_table
------------ ----------------------------- -------------
Dana Tanaka dana.tanaka25@school.example 1
Hugo Hassan NULL 3
Isla Mohamed isla.mohamed26@school.example 2
Kira Perez kira.perez17@school.example 1
Nico Diaz nico.diaz23@school.example 2
Omar Ito omar.ito14@school.example 2
Paulo Park NULL 1
Sven Okafor sven.okafor22@school.example 1
Sven Silva sven.silva29@school.example 1
(9 rows)
A teacher's name and email appear once per enrolled student.
This repetition is called redundancy. It wastes space, but the real damage is that repeated facts can disagree. The problems it causes are called anomalies.
2. The update anomaly
A fact changes. If you update only one row, the table now says two different things:
Dana Costa changes her phone number, but only one row gets the new one:
UPDATE orders_flat SET phone = '+58 00000000' WHERE order_id = 451 AND line_no = 1;
SELECT DISTINCT customer, phone FROM orders_flat WHERE customer = 'Dana Costa' ORDER BY phone;
customer phone
---------- ------------
Dana Costa +58 00000000
Dana Costa +58 13864688
(2 rows)
The Central branch moves to Porto, but only one row gets the new city:
UPDATE accounts_flat SET branch_city = 'Porto' WHERE account_id = 1;
SELECT DISTINCT branch_name, branch_city FROM accounts_flat WHERE branch_name = 'Central' ORDER BY branch_city;
branch_name branch_city
----------- -----------
Central Lisbon
Central Porto
(2 rows)
Isla Mohamed gets a new email address, but only one row gets it:
UPDATE enrollment_flat SET teacher_email = 'isla.new@school.example' WHERE student = 'Vera Khan' AND course = 'ART101';
SELECT DISTINCT teacher, teacher_email FROM enrollment_flat WHERE teacher = 'Isla Mohamed' ORDER BY teacher_email;
teacher teacher_email
------------ -----------------------------
Isla Mohamed isla.mohamed26@school.example
Isla Mohamed isla.new@school.example
(2 rows)
Which one is right? The data cannot tell you. To change one fact you must change every copy, and one missed copy means damaged data.
3. The insert anomaly
A new fact arrives that has no row to live in. In this table, a customer, branch or teacher exists only inside a row about something else. If the table requires that other part (as a real one would, with NOT NULL), the new fact is refused:
A new customer, Amy, signs up but has not ordered anything. Where do you store her?
CREATE TABLE orders_strict (
order_id INTEGER PRIMARY KEY,
customer TEXT NOT NULL, phone TEXT, city TEXT,
product TEXT NOT NULL, price REAL NOT NULL
);
(done)
INSERT INTO orders_strict (customer, phone, city) VALUES ('Amy Reza', '+44 11111111', 'Leeds');
IntegrityError: NOT NULL constraint failed: orders_strict.product
A new branch, Lakeside, opens but has no accounts yet. Where do you store it?
CREATE TABLE accounts_strict (
account_id INTEGER PRIMARY KEY,
customer TEXT NOT NULL, account_type TEXT NOT NULL,
branch_name TEXT, branch_city TEXT
);
(done)
INSERT INTO accounts_strict (branch_name, branch_city) VALUES ('Lakeside', 'Accra');
IntegrityError: NOT NULL constraint failed: accounts_strict.customer
A new course section is planned with a teacher, but no student has enrolled yet. Where do you store it?
CREATE TABLE enrollment_strict (
id INTEGER PRIMARY KEY,
student TEXT NOT NULL, course TEXT NOT NULL,
teacher TEXT, teacher_email TEXT
);
(done)
INSERT INTO enrollment_strict (course, teacher) VALUES ('CS301', 'Hugo Hassan');
IntegrityError: NOT NULL constraint failed: enrollment_strict.student
You cannot record one fact without inventing another.
4. The delete anomaly
Deleting rows about one thing destroys facts about another:
Femi Bianchi's only order is cancelled and deleted. Femi's phone and city are gone with it:
DELETE FROM orders_flat WHERE customer = 'Femi Bianchi';
SELECT DISTINCT customer, phone, city FROM orders_flat WHERE customer = 'Femi Bianchi';
customer phone city
-------- ----- ----
(0 rows)
The only account of the Harbour branch in this table is closed and deleted. The branch itself is gone with it:
DELETE FROM accounts_flat WHERE account_id = 11;
SELECT DISTINCT branch_name, branch_city FROM accounts_flat WHERE branch_name = 'Harbour';
branch_name branch_city
----------- -----------
(0 rows)
Quinn Meyer (student 4) leaves school, and the only row about the SCI101 course goes with them. The course and its teacher vanish:
DELETE FROM enrollment_flat WHERE student = 'Quinn Meyer';
SELECT DISTINCT course, teacher FROM enrollment_flat WHERE course = 'SCI101';
course teacher
------ -------
(0 rows)
Deleting one fact (an order, an account, a student) destroyed another (a customer, a branch, a teacher).
5. The cure: one fact, one place
All three anomalies have one root cause: two different kinds of fact are mixed in one table. Give each kind its own table and link them with a key (chapter 3). The sample database is built that way already, so here is the same three jobs done on the real tables:
The phone is stored once, in customers, so there is nothing to disagree with:
UPDATE customers SET phone = '+58 00000000' WHERE customer_id = 3;
SELECT c.first_name, c.phone, o.order_id FROM customers c JOIN orders o ON o.customer_id = c.customer_id WHERE c.customer_id = 3 ORDER BY o.order_id LIMIT 4;
first_name phone order_id
---------- ------------ --------
Dana +58 00000000 451
Dana +58 00000000 1039
Dana +58 00000000 1379
Dana +58 00000000 1796
(4 rows)
A customer with no orders is fine, and deleting an order leaves the customer alone:
INSERT INTO customers (first_name, last_name, email, city, country, joined_on)
VALUES ('Amy', 'Reza', 'amy.reza@example.com', 'Leeds', 'United Kingdom', '2026-10-01');
DELETE FROM order_items WHERE order_id = 2332;
DELETE FROM payments WHERE order_id = 2332;
DELETE FROM orders WHERE order_id = 2332;
SELECT customer_id, first_name, last_name, phone FROM customers WHERE customer_id IN (93, 421) ORDER BY customer_id;
customer_id first_name last_name phone
----------- ---------- --------- ------------
93 Femi Bianchi +41 11471941
421 Amy Reza NULL
(2 rows)
The city is stored once, in branches, so there is nothing to disagree with:
UPDATE branches SET city = 'Porto' WHERE name = 'Central';
SELECT a.account_id, b.name, b.city FROM accounts a JOIN branches b ON b.branch_id = a.branch_id WHERE a.account_id IN (1, 4, 12) ORDER BY a.account_id;
account_id name city
---------- ------- -----
1 Central Porto
4 Central Porto
12 Central Porto
(3 rows)
A branch with no accounts is fine, and deleting an account leaves the branch alone:
INSERT INTO branches (name, city) VALUES ('Lakeside', 'Accra');
INSERT INTO accounts (customer_id, branch_id, account_type, opened_on, balance_cents) VALUES (1, 7, 'savings', '2026-10-01', 0);
DELETE FROM accounts WHERE account_id = 451;
SELECT branch_id, name, city FROM branches WHERE name = 'Lakeside';
branch_id name city
--------- -------- -----
7 Lakeside Accra
(1 row)
The email is stored once, in teachers, so there is nothing to disagree with:
UPDATE teachers SET email = 'isla.new@school.example' WHERE name = 'Isla Mohamed';
SELECT st.last_name, c.code, t.name, t.email
FROM enrollments e
JOIN students st ON st.student_id = e.student_id
JOIN sections sec ON sec.section_id = e.section_id
JOIN courses c ON c.course_id = sec.course_id
JOIN teachers t ON t.teacher_id = sec.teacher_id
WHERE t.name = 'Isla Mohamed'
ORDER BY st.student_id, c.code
LIMIT 3;
last_name code name email
--------- ------ ------------ -----------------------
Khan ART101 Isla Mohamed isla.new@school.example
Garcia ART101 Isla Mohamed isla.new@school.example
Rossi ART101 Isla Mohamed isla.new@school.example
(3 rows)
A section with no students is fine, and a student leaving does not remove the section or its teacher:
INSERT INTO sections (course_id, teacher_id, term, room, capacity) VALUES (19, 26, '2026-Autumn', 'R01', 30);
DELETE FROM enrollments WHERE student_id = 4;
SELECT section_id, course_id, term FROM sections WHERE section_id IN (7, 42) ORDER BY section_id;
section_id course_id term
---------- --------- -----------
7 4 2025-Spring
42 19 2026-Autumn
(2 rows)
Nothing was lost and nothing disagrees. The rest of this chapter teaches the step-by-step method for finding such splits: normalization.
Worked example: spot the redundancy
A second wide table lists every product with its supplier's name and country:
CREATE TABLE product_flat AS
SELECT p.name AS product, s.name AS supplier, s.country AS supplier_country
FROM products p
JOIN suppliers s ON s.supplier_id = p.supplier_id;
SELECT COUNT(*) AS rows_in_table, COUNT(DISTINCT supplier) AS different_suppliers FROM product_flat;
rows_in_table different_suppliers
------------- -------------------
45 10
(1 row)
- Repeated: every supplier's name and country, once per product it supplies.
- Update anomaly: a supplier moves to another country; one forgotten row gives two countries.
- Insert anomaly: a new supplier with no products yet cannot be stored.
- Delete anomaly: if a supplier's last product is removed, the supplier vanishes.
- Cure: a
supplierstable (name, country) and aproductstable with asupplier_id.
A second wide table lists every card with the branch of its account:
CREATE TABLE card_flat AS
SELECT k.card_id, k.last4, a.account_type, b.name AS branch_name, b.city AS branch_city
FROM cards k
JOIN accounts a ON a.account_id = k.account_id
JOIN branches b ON b.branch_id = a.branch_id;
SELECT COUNT(*) AS rows_in_table, COUNT(DISTINCT branch_name) AS different_branches FROM card_flat;
rows_in_table different_branches
------------- ------------------
250 6
(1 row)
- Repeated: every branch's name and city, once per card.
- Update anomaly: a branch moves; one forgotten row gives two cities.
- Insert anomaly: a new branch with no cards yet cannot be stored.
- Delete anomaly: if a branch's last card is cancelled, the branch vanishes.
- Cure: a
branchestable (name, city) and acardstable that points to the account, and the account to the branch.
A second wide table lists every section with the course title and its department:
CREATE TABLE section_flat AS
SELECT sec.section_id, sec.term, c.code AS course_code, c.title AS course_title, d.name AS department, d.building
FROM sections sec
JOIN courses c ON c.course_id = sec.course_id
JOIN departments d ON d.department_id = c.department_id;
SELECT COUNT(*) AS rows_in_table, COUNT(DISTINCT course_code) AS different_courses FROM section_flat;
rows_in_table different_courses
------------- -----------------
42 18
(1 row)
- Repeated: every course's title, department and building, once per section.
- Update anomaly: a department moves to another building; one forgotten row gives two buildings.
- Insert anomaly: a new course with no section yet cannot be stored.
- Delete anomaly: if a course's last section is cancelled, the course vanishes.
- Cure: a
coursestable (code, title, department) and asectionstable that points to the course.
(All data is made up.)
Common mistakes
- Thinking redundancy is only about disk space. The real cost is data that contradicts itself.
- Fixing it by "being careful". Careful people still forget one row; a design that stores a fact once cannot disagree with itself.
- Splitting into tables without a key to link them. The pieces must stay joinable (chapter 3).
- Calling every repeated value redundancy.
Lisbonappearing twice in acitycolumn is fine when it is two different people; redundancy is when the same fact is stored twice.
Key takeaways
- Redundancy means the same fact is stored in more than one place.
- It causes update, insert and delete anomalies.
- The root cause is mixing different kinds of fact in one table.
- The cure is one fact in one place, with tables linked by keys.
- Normalization is the method for finding those splits.
Practice
1. What is an update anomaly?
Show answer
Changing a repeated fact in some rows but not others, leaving the data contradicting itself.
2. Why was the new fact refused in the one-table design of section 3?
Show answer
Customers existed only inside order rows, and an order row needs a product, so a customer without an order could not be stored.
Show answer
Branches existed only inside account rows, and an account row needs a customer, so a branch without an account could not be stored.
Show answer
Sections existed only inside enrollment rows, and an enrollment row needs a student, so a section without students could not be stored.
3. Which anomaly lost the facts in section 4?
Show answer
The delete anomaly: deleting Femi's only order deleted the only copy of Femi's phone and city.
Show answer
The delete anomaly: deleting the only account of Harbour deleted the only copy of the branch's name and city.
Show answer
The delete anomaly: deleting the only enrollment in SCI101 deleted the only copy of the course and its teacher.
4. Which two tables would you create to remove the redundancy of section 1?
Show answer
customers (customer_id, name, phone, city) and orders lines that point to the customer with customer_id.
Show answer
branches (branch_id, name, city) and accounts rows that point to the branch with branch_id.
Show answer
teachers (teacher_id, name, email) and enrollments or sections that point to the teacher with teacher_id.
5. Is a city appearing in two customer (or branch) rows redundancy?
Show answer
No. They are two different people or places that happen to be in the same city. Redundancy is the same fact stored twice.
6. In the fixed design, how many places must you change when one of those facts changes?
Show answer
One: its row in the table that owns the fact (customers, branches or teachers).