Keys
After this: Pick a primary key, watch the database refuse a repeated one, and follow a column that points at another table.
On this page · 5 sections · 3 checks
Suppose a second student called Ana joins the school. A teacher writes "Ana's mark is 72". Which Ana? A name is not enough to pick out one student. The school needs something that is different for every student, always.
A value that names one row
Look at the id column of the students table: 1, 2, 3, 4, 5, 6. No two rows share a number, so a number names exactly one student. Number 3 is Chloe, and only Chloe.
A column like this is called a , or the key for short. Test each column of the students table:
| column | could it be the key? |
|---|---|
id | yes: every row has a different number, and the school gives each new student a new one |
name | no: the six names differ today, but a second Ana could join tomorrow |
year | no: Ana and Chloe are both in year 2 |
city | no: Ana, Chloe and Femi all live in Leeds |
So a key must be different in every row that could ever be added, not only in today's rows. That is why many tables use a number made up for the purpose, like id.
The database guards the key
The table itself says which column is the key. Here is the students table as the database was told to make it:
CREATE TABLE students ( id INTEGER PRIMARY KEY, name TEXT NOT NULL, year INTEGER NOT NULL CHECK (year BETWEEN 1 AND 3), city TEXT NOT NULL);The words PRIMARY KEY on the id line make id the key. The other words (INTEGER, TEXT, NOT NULL, CHECK) are rules too; the "Designing tables" tutorial explains each one.
Because id is the key, the database checks it every time a row is added. The block below asks to add a new student, Zed, with the id 3. INSERT INTO asks for a new row; you will write your own in the "Changing data and transactions" tutorial. Press Run.
The database refuses, with the message UNIQUE constraint failed: students.id. Read it in three parts:
- A is a rule written into a table, which the database checks on every change.
UNIQUEis the kind of rule: no two rows may share the value. A primary key carries this rule.students.idis where the rule lives: theidcolumn of the students table.
Nothing was added. Chloe keeps her row, and the table still has six rows. Now change the 3 to 7 and run it again, or run the next block, which also lists the students afterwards.
Seven rows: Zed is in, with a number of his own.
A column that points at another table
Go back to the courses table from lesson 1. Its last column, teacher_id, holds a teacher's key. Find the courses Mrs Okafor teaches:
- Find her key in the teachers table: Mrs Okafor's
idis 1. - Go through the courses and keep the rows whose
teacher_idis 1: Maths and Physics.
One teacher, many courses: each course names one teacher, and a teacher can be named by several courses. A column like teacher_id is called a : it holds the key of a row in another table. The enrolments table has two of them, student_id and course_id. The "Designing tables" tutorial shows how a table tells the database to check that every one of them points at a row that exists.
A key can also be two columns together. The enrolments table has no id; its key is the pair student_id and course_id. Ana (1) appears in three rows and Maths (1) in four, but the pair 1 and 1 may appear only once. The database refuses a second row for the same student and the same course, so a student cannot take the same course twice.
Check yourself
Which column of the students table could be its primary key?
Femi already has the id 6. What happens when this runs on the school tables?
INSERT INTO students (id, name, year, city)VALUES (6, 'Gus', 2, 'Hull');Mr Silva's id in the teachers table is 2. Using the courses table from lesson 1, which courses does he teach?
Honesty notes
- Say what a primary key is: a column whose value is different in every row, so one value names one row.
- Test whether a column could be a key: id can, while city, year and even name cannot, because a value could repeat.
- Read the error for a repeated key, UNIQUE constraint failed: students.id, and say that nothing was added.
- Follow a foreign key such as teacher_id from a course to its teacher, and say that a key can be two columns together.