Rules the database keeps
After this: Write NOT NULL, UNIQUE and CHECK on a column, and read the message SQLite gives when a row breaks one.
On this page · 5 sections · 3 checks
A school office types in marks all day. One day someone types 140 for a test marked out of 100. Another day a new student is saved with no name. Who should notice?
Your program could test every value before it saves it. But a school has many programs that write to the same database: the office app, the teachers' app, a script that loads last year's marks. Each one would need the same tests, and one forgotten test lets a bad row in. It is safer when the database itself keeps the rules, for every program at once.
A rule on a column
When you create a table, you can write a rule next to a column. From then on, the database tests every row that is added or changed, and it refuses a row that breaks the rule. Such a rule is a .
Here is the students table of the school, as it was created:
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);Read it one column at a time:
id INTEGER PRIMARY KEY: the student's number, different in every row. Keys are the next lesson.name TEXT NOT NULL: every student has a name.NOT NULLrefuses a row whose name is missing.year INTEGER NOT NULL CHECK (year BETWEEN 1 AND 3): the year must be there, and it must pass a test: it must be 1, 2 or 3.city TEXT NOT NULL: every student has a city.
A third kind of rule sits in the courses table: title TEXT NOT NULL UNIQUE. UNIQUE means that no two rows may have the same value in that column.
Each rule refusing a row
Each block below is real SQLite, running in your browser. It loads the school, then tries to add one bad row. Press Run and read SQLite's message in the warning box. Every Run starts again from the same school, so nothing you add carries over.
NOT NULL: a student with no name.
UNIQUE: a second course called Maths.
CHECK: a student in year 4.
The three messages have one shape: the kind of rule, then where it failed. NOT NULL and UNIQUE name the table and the column, as in students.name or courses.title. CHECK repeats its test, year BETWEEN 1 AND 3, because one test may use several columns.
When a row is refused, no part of it is stored. The table stays as it was before the statement.
The enrolments table has a CHECK on marks too, and it refuses 101:
A rule only where you write one
mark has a CHECK but no NOT NULL. That is on purpose: Chloe's History mark is missing because her work is not marked yet. So a row without a mark goes in. This block enrols Eve (student 5) in Physics (course 2) with no mark, then reads her row back.
The mark comes back as NULL. Why did the CHECK not refuse it? A CHECK refuses a row only when its test is false. For a missing mark, mark BETWEEN 0 AND 100 has no answer at all, so the row passes. When a value must be there, write NOT NULL as well, as year does.
The general rule: for each column, decide whether it may be missing (NOT NULL or not), whether two rows may share a value (UNIQUE or not), and which values make sense (CHECK).
Check yourself
A teacher types a mark of 140 for Ben in Maths. Which rule in the enrolments table refuses the row?
The school already has a course called Maths, and courses.title is written TEXT NOT NULL UNIQUE. What does SQLite say to this statement?
INSERT INTO courses (id, title, credits, teacher_id)VALUES (6, 'Maths', 3, 2);In the students table, year is written INTEGER NOT NULL CHECK (year BETWEEN 1 AND 3). What happens to this row, with no year?
INSERT INTO students (id, name, year, city)VALUES (7, 'Gus', NULL, 'Bath');Honesty notes
- Say what a constraint is: a rule written into a table, which the database tests on every row that is added or changed.
- Write NOT NULL for a value that must be there, UNIQUE for a value no two rows may share, and CHECK for a test each value must pass.
- Read SQLite's message for a refused row: the kind of rule, then the table and column, or the CHECK's own test.
- Say why a missing mark passes CHECK (mark BETWEEN 0 AND 100): a CHECK refuses only when its test is false, so a value that must be there also needs NOT NULL.