Keys and references
After this: Give a table a key of one or two columns, point one table at another with a foreign key, and switch foreign keys on in SQLite.
On this page · 6 sections · 4 checks
Two students called Ana join the same school. When a teacher writes "Ana got 72 in Maths", which Ana is that? A name cannot tell them apart. A student number can. The last lesson put rules on single columns. This lesson is about the two rules that name rows and connect tables: keys and references.
A key names one row
The school gives every student a number that no other student has. A column like that is the table's : its value names exactly one row. In the students table it is id INTEGER PRIMARY KEY.
A primary key keeps two promises: every row has one, and no two rows share one. So a second student number 3 is refused:
You do not have to choose the number yourself. In SQLite, when you leave out an INTEGER PRIMARY KEY, the database picks one: usually one more than the largest id in the table. The school has six students, so Gus becomes number 7.
A key made of two columns
The enrolments table records who takes what. Ana takes three courses, so her student_id (1) is in three rows. Maths has four students, so its course_id (1) is in four rows. Neither column alone is different in every row.
The pair is. A student can be enrolled in a course only once, so (1, 1), Ana in Maths, can be in one row only. A key made of several columns is a . It is written on a line of its own, after the columns:
CREATE TABLE enrolments ( student_id INTEGER NOT NULL REFERENCES students(id), course_id INTEGER NOT NULL REFERENCES courses(id), mark INTEGER CHECK (mark BETWEEN 0 AND 100), PRIMARY KEY (student_id, course_id));Enrolling Ana in Maths a second time is refused, and the message names both columns:
A reference to another table
courses.teacher_id holds the id of the teacher who gives the course: Maths has 1, which is Mrs Okafor. The words REFERENCES teachers(id) after it are a promise: every value in this column is the id of a row that exists in teachers. A column with that promise is a . The two id columns of enrolments are foreign keys too, to students and to courses.
Press Run to step through the school's tables drawn as boxes, with an arrow for each foreign key.
Press Run it to build the diagram one step at a time.
- classes
- 0
- relations
- 0
A course taught by teacher 9 is refused, because there is no teacher 9:
The message is shorter than the others: it does not say which column failed. You find it by reading the statement.
Switch the check on
SQLite does not test foreign keys unless you ask. Every connection to the database has to run this line first:
PRAGMA foreign_keys = ON;It is the first line of the school's schema, so the blocks above have all run it. Here is the same course with that line left out. It goes in, and the database now points at a teacher who does not exist:
The switch belongs to the connection, not to the file. A program that opens the database must run the PRAGMA line each time it connects. Remember it when you use a database from your own code, in the last tutorial of this learning path.
Number keys or name keys
Why give rows numbers at all, when every course already has a title? Two reasons:
- Names can repeat. Two students may both be called Ana.
- Names change. If History is renamed Modern History, every row that points at it by name must change too. A number means nothing on its own, so it never has to change.
Lesson 4 uses names as keys for a while, because names are easier to read while you split a table. At the end it swaps them for numbers.
Check yourself
The enrolments table has the columns student_id, course_id and mark. What can be its primary key?
Complete the last line of the enrolments table, so that the pair of student and course is its key, and enrolling Ana in Maths a second time is refused.
CREATE TABLE enrolments ( student_id INTEGER NOT NULL REFERENCES students(id), course_id INTEGER NOT NULL REFERENCES courses(id), mark INTEGER CHECK (mark BETWEEN 0 AND 100), ________________ -- ← one key made of both columns);Ms Chen is teacher 3, and the course Music has teacher_id 3. Foreign keys are switched on. What does SQLite say to this statement?
DELETE FROM teachers WHERE id = 3;A program opens the school database without running PRAGMA foreign_keys = ON, then adds a course with teacher_id 9. There is no teacher 9. What happens?
Honesty notes
- Give a table a primary key: a column whose value is different in every row, so that it names one row.
- Write a key made of two columns, PRIMARY KEY (student_id, course_id), when no single column is different in every row.
- Connect two tables with a foreign key, written with REFERENCES, and read the FOREIGN KEY constraint failed message.
- Switch foreign keys on in SQLite with PRAGMA foreign_keys = ON, on every connection; without it SQLite checks no reference.
- Say why number keys are safer than names: names repeat and change, numbers do neither.