Splitting step by step
After this: Split the flat table by the second and third rules, and join the four pieces back into the 7 rows you started with.
On this page · 8 sections · 3 checks
Lesson 3 ended with flat: one value in every cell, and still Ana's city written 3 times and room R1 written 4 times. Which columns should move out, and where to? Two more rules answer that. Both ask one question about each column: what decides its value?
What decides a value
Take one row of flat: Ana, Leeds, Maths, Mrs Okafor, R1, 72. For each value, ask what you need to know to fill it in.
- Leeds: you need only the student. Ana lives in Leeds whatever the course.
- Mrs Okafor: you need only the course. Maths is taught by Mrs Okafor whoever takes it.
- R1: you need only the teacher. Mrs Okafor teaches in R1.
- 72: you need both the student and the course. Ana has 72 in Maths and 65 in Physics.
When knowing one column is enough to know another, the second the first. City depends on student; mark depends on the pair (student, course).
That pair is also the key of flat: each student and course appear together in one row only.
The second rule: depend on the whole key
A table is in (2NF) when every column outside the key depends on the whole key, not on part of it. In flat:
markdepends on student and course together. It stays, in a table called enrolments.citydepends on the student alone. It moves to a table students(name, city), 3 rows.teacherdepends on the course alone. It moves to a table courses(title, teacher), 4 rows.roomalso follows the course, through its teacher, so for now it moves to courses with the teacher.
- enrolments (*) is linked to 1 students
- enrolments (*) is linked to 1 courses
Each fact about a student is now in one row. Run the block to build courses at this stage. SELECT DISTINCT keeps one copy of each different row, so the 7 rows of flat give 4 courses:
R1 is still written twice, for Maths and for Physics.
The third rule: depend on nothing but the key
The key of courses is the title. Knowing the title does tell you the room, but only through the teacher: Maths gives Mrs Okafor, and Mrs Okafor gives R1. Knowing the teacher alone is already enough to know the room, and the teacher is not the key of courses. A table is in (3NF) when no column outside the key depends on another column outside the key. So room moves out, with the teacher's name as the key of a new table:
courses keeps title and teacher, and its teacher column becomes a foreign key to teachers.
- courses (*) is linked to 1 teachers
- enrolments (*) is linked to 1 students
- enrolments (*) is linked to 1 courses
The whole split
Here is the split in full. It creates the four tables with their keys and foreign keys, then fills each one from flat:
PRAGMA foreign_keys = ON;
CREATE TABLE students ( name TEXT NOT NULL PRIMARY KEY, city TEXT NOT NULL);
CREATE TABLE teachers ( name TEXT NOT NULL PRIMARY KEY, room TEXT NOT NULL);
CREATE TABLE courses ( title TEXT NOT NULL PRIMARY KEY, teacher TEXT NOT NULL REFERENCES teachers(name));
CREATE TABLE enrolments ( student TEXT NOT NULL REFERENCES students(name), course TEXT NOT NULL REFERENCES courses(title), mark INTEGER CHECK (mark BETWEEN 0 AND 100), PRIMARY KEY (student, course));
INSERT INTO students (name, city) SELECT DISTINCT student, city FROM flat;INSERT INTO teachers (name, room) SELECT DISTINCT teacher, room FROM flat;INSERT INTO courses (title, teacher) SELECT DISTINCT course, teacher FROM flat;INSERT INTO enrolments (student, course, mark) SELECT student, course, mark FROM flat;The first three INSERTs use SELECT DISTINCT, so each student, teacher and course is stored once. The last one copies all 7 rows, because each enrolment is a different fact. The name keys get NOT NULL, for the reason in lesson 2's honesty note.
Check the split: join it back
A split is right only when no row was lost and none was made up. Join the four tables back together on their keys, and you should get the 7 rows of flat, exactly.
SELECT e.student, s.city, e.course, c.teacher, t.room, e.markFROM enrolments AS eJOIN students AS s ON s.name = e.studentJOIN courses AS c ON c.title = e.courseJOIN teachers AS t ON t.name = c.teacherORDER BY e.student, e.course;enrolments · 7 rows
| student | course | mark |
|---|---|---|
| Ana | Maths | 72 |
| Ana | Physics | 65 |
| Ana | History | 80 |
| Ben | Maths | 58 |
| Ben | Art | 90 |
| Chloe | Maths | 85 |
| Chloe | History | NULL |
students · 3 rows
| name | city |
|---|---|
| Ana | Leeds |
| Ben | York |
| Chloe | Leeds |
courses · 4 rows
| title | teacher |
|---|---|
| Maths | Mrs Okafor |
| Physics | Mrs Okafor |
| History | Mr Silva |
| Art | Mr Silva |
teachers · 2 rows
| name | room |
|---|---|
| Mrs Okafor | R1 |
| Mr Silva | R2 |
Press Run it, then step through the query one clause at a time, in the order SQLite works.
- rows
- 0
- columns
- 0
Run the join, then the same rows read from flat. Both blocks give the same 7 rows in the same order:
Now each fact can change in one place. Moving Mrs Okafor to R5 is one row of teachers, and all four of her enrolments see it:
The traps from lesson 3 are gone too. Music can be a row of courses before anyone enrols, and Ben's city stays in students if he leaves every course.
The last step: number keys
Lesson 2 gave two reasons to prefer numbers as keys. Swap each name key for an id number, and each name column in enrolments and courses for a student_id, course_id or teacher_id, then add the columns the school also keeps, such as year and credits. You now have the school's four tables from lesson 2, the ones this whole learning path has used.
Check yourself
The key of flat is the pair (student, course). Which column breaks the second rule, because it depends on only part of the key?
After the second rule, courses has the columns title (the key), teacher and room. Which column breaks the third rule?
After the split, students has 3 rows, teachers 2, courses 4 and enrolments 7. How many rows does the join back give?
Honesty notes
- Say what a column depends on: the column or columns whose value is enough to know it.
- Apply the second rule: a column outside the key that depends on only part of the key moves to a table of its own.
- Apply the third rule: a column that depends on another column outside the key moves out with that column as the key.
- Build the split with CREATE TABLE and INSERT INTO … SELECT DISTINCT, and check it by joining the pieces back into the 7 rows of the flat table.
- Swap name keys for number keys at the end, which gives the school's four tables.