Why the data is in several tables
After this: Follow a teacher id from a course row to the teacher's name, and say why the name is stored once.
On this page · 5 sections · 3 checks
The school office wants a list of every course with the name of the teacher who teaches it. That sounds like one query on the courses table. But when you look at a course row, the teacher's name is not there. The row holds a number instead. This tutorial is about putting that number and the name back together.
Where is the teacher's name?
Here is the courses table, with the columns that matter for now. Press Run.
Each course has a teacher_id: Maths and Physics say 1, History and Art say 2, Music says 3. The names live in a different table, teachers:
Looking it up by hand
Who teaches History? You can answer with the two tables above, one step at a time.
- Find History in the courses table. Its row is
(3, 'History', 2), so itsteacher_idis 2. - Find the row whose
idis 2 in the teachers table. It is(2, 'Mr Silva', 'R2'). - So Mr Silva teaches History, in room R2.
The second step is a query you already know how to write:
Why not write the name in every course row?
It would be shorter to put the teacher's name and room into each course row. Think about what that does to Mrs Okafor, who teaches two courses:
| title | teacher | room |
|---|---|---|
| Maths | Mrs Okafor | R1 |
| Physics | Mrs Okafor | R1 |
Her name and room are now written twice. If she moves to room R4, two rows must change. If someone changes one row and forgets the other, the table says two different things about one person, and nothing tells you which is true. A typing mistake in one row would even make her look like two different teachers.
With the number instead, her name and room are stored once, in the teachers row whose id is 1. Each course that she teaches points at that row with the number 1. A change to her room is a change to one row, and every course sees it.
A is a column like teacher_id: its value is the key of a row in another table. The "Designing tables" tutorial shows how to make the database refuse a foreign key that points at nothing. For now, read it as an arrow from one row to another.
The four tables and their arrows
The school has four tables, and three columns point from one table to another:
| column | points at | read it as |
|---|---|---|
courses.teacher_id | teachers.id | who teaches this course |
enrolments.student_id | students.id | which student this enrolment is for |
enrolments.course_id | courses.id | which course this enrolment is for |
courses.teacher_id means "the column teacher_id of the table courses". The table name, a dot, then the column name. You will write column names this way often in this tutorial, because two tables can have columns with the same name: both teachers and students have a column called name.
Doing the lookup by hand works for one course. For all five courses, or for ten enrolments, you want the database to do it. A is a query that does exactly that: it puts a row of one table next to the row of another table that belongs with it. The next lesson writes the first one.
Check yourself
The courses table has the row (2, 'Physics', 1): id 2, title Physics, teacher_id 1. The teachers table has the rows (1, 'Mrs Okafor', 'R1'), (2, 'Mr Silva', 'R2') and (3, 'Ms Chen', 'R3'). Who teaches Physics?
Mrs Okafor teaches two courses. Why does each of her course rows hold her id, 1, rather than her name and room?
The enrolments table has the columns student_id, course_id and mark. Which of them point at a row in another table?
- Say why a course row holds a teacher's id rather than the teacher's name: the name and room are then stored once, and cannot disagree.
- Follow a number like teacher_id to the row it names in another table, in two steps, by hand.
- Say what a foreign key is: a column whose value is the key of a row in another table.
- Name the three arrows of the school: courses to teachers, and enrolments to students and to courses.