One fact in one place
After this: Put one value in each cell, and see how a fact written in several rows can come to disagree with itself.
On this page · 5 sections · 3 checks
The last two lessons gave each table rules about its values. A table can keep every rule and still be badly shaped. This lesson starts from a table a teacher might really make, and shows the two problems in it.
A list in one cell
A teacher collects sign-ups in a table with one row per student. The courses go in one cell, with commas between them:
| student | city | courses |
|---|---|---|
| Ana | Leeds | Maths, Physics, History |
| Ben | York | Maths, Art |
| Chloe | Leeds | Maths, History |
It looks tidy. Now ask it a question: who takes Maths? The courses are inside a piece of text, so the query has to search inside that text with LIKE:
Ana, Ben and Chloe: right, so far. Then Dev signs up for Physics, Art and Applied Maths. Run the same question again with Dev's row added:
Dev does not take Maths, but '%Maths%' matches the letters inside "Applied Maths". The table cannot tell one course from another, because it never held courses: it held text. Other questions are hard too. Counting the students in History means cutting text apart, and the database cannot test that each course in the list exists.
The cure is a rule about shape: one value in each cell. A table that keeps it is in , often written 1NF. The normal forms are a short series of such rules. Each one removes one more way for a table to store the same fact twice.
One row per student and course
To keep the first rule, give each student and course its own row. The teacher also adds the teacher of each course, the room and the mark:
Every cell now holds one value, and "who takes Maths?" is WHERE course = 'Maths', which cannot match Applied Maths. This table is in first normal form. Call it flat.
The same fact, written many times
Look along the columns of flat and count:
- Ana lives in Leeds. That one fact is written 3 times, once for each of her courses.
- Mrs Okafor teaches in room R1. That fact is written 4 times, once for each enrolment in her courses.
Each extra copy can come to disagree with the others. Suppose Ana moves to York and the office changes only the row it is looking at, her Maths row:
Step by step:
- The UPDATE changes one row: Ana in Maths now says York.
- Her Physics and History rows still say Leeds.
- "Where does Ana live?" now has two answers, Leeds and York. Neither SQLite nor any rule from lessons 1 and 2 can say which is right.
This is an : a change that reaches some copies of a fact and misses others.
The safe change touches every copy. Moving Mrs Okafor to room R5 must change all four of her rows:
That works, as long as everyone who changes the table remembers every copy. The table itself does nothing to help.
There are two more problems of the same kind. Music has no students yet, so flat has no row to hold the fact that Ms Chen teaches it. And if Ben left both of his courses, deleting his rows would also delete the only record of his city.
The general rule: when one fact is written in many rows, the table can come to disagree with itself. Lesson 4 splits flat so that each fact is written in one place.
Check yourself
Which of these tables breaks the first rule, one value in each cell?
Mrs Okafor moves from room R1 to room R5. How many rows of the flat table must change so that the table stays right?
Someone moves the Maths rows to room R9 but forgets Physics. Which rows does the SELECT return?
UPDATE flat SET room = 'R9'WHERE course = 'Maths';
SELECT DISTINCT roomFROM flatWHERE teacher = 'Mrs Okafor'ORDER BY room;Honesty notes
- Keep the first rule, one value in each cell, and say why a list in a cell makes LIKE find Applied Maths when you ask for Maths.
- Count how many times a table writes one fact: Ana's city 3 times and room R1 4 times in the flat table.
- Say what an update anomaly is: a change that reaches some copies of a fact and misses others, so the table gives two answers.
- Name the two other traps of a repeated fact: no row to hold a course with no students, and a city lost when a student's last row is deleted.