OCR H446, section 1.3.2 (e)
What is referential integrity?
In a relational database, tables are linked by foreign keys. Referential integrity is the rule that every foreign key value must match an existing primary key value in the table it refers to (or be empty, if that is allowed).
In other words, a record can't refer to something that doesn't exist.
Example:
AUTHOR
| AuthorID | Name |
|---|---|
| 10 | Frank Herbert |
| 11 | Jane Austen |
BOOK
| BookID | Title | AuthorID (FK) |
|---|---|---|
| 1 | Dune | 10 |
| 2 | Emma | 11 |
| 3 | Mystery Book | 99 |
The third book refers to author 99, who isn't in the AUTHOR table. That breaks referential integrity. The record is an orphan: it points to nothing. A query joining the tables would lose this book, or give wrong results.
Why it matters
- It keeps the data consistent and meaningful.
- It prevents orphan records and broken links.
- It makes sure that joins give correct, complete results.
- It protects the accuracy and reliability of the database.
How it is enforced
The database management system (DBMS) checks the rule automatically whenever data is changed, if the foreign key is declared:
CREATE TABLE Book (
BookID INTEGER PRIMARY KEY,
Title VARCHAR(50),
AuthorID INTEGER,
FOREIGN KEY (AuthorID) REFERENCES Author(AuthorID)
);
You don't need to write this; it shows how the database is told about the link.
Insert
Adding a record with a foreign key value that doesn't exist in the referenced table is refused.
-- refused: there is no author 99
INSERT INTO Book VALUES (3, 'Mystery Book', 99);
To add the book, the author must be added first.
Update
- Changing a foreign key to a value that doesn't exist is refused.
- Changing a primary key that is referred to by other records would orphan them, so it is refused, or the change is passed on to the related records.
Delete
Deleting a record that other records refer to would leave orphans. For example, deleting Jane Austen (11) while Emma refers to her.
DELETE FROM Author WHERE AuthorID = 11; -- would orphan Emma
The DBMS can respond in different ways, depending on how it was set up:
| Option | What happens |
|---|---|
| Restrict (refuse) | The deletion is not allowed while records still refer to it |
| Cascade | The related records are deleted as well (Emma would be deleted too) |
| Set to null | The foreign keys in the related records are made empty |
Cascade is convenient, but dangerous, since a single delete can remove a lot of data.
Deleting an author:
Restrict -> refused: books still refer to this author
Cascade -> author AND all their books are deleted
Set null -> author deleted, their books keep AuthorID = empty
Entity integrity
A related idea is entity integrity: a primary key must be unique and can't be empty (null), so that every record can be identified.
Worked example
Task: a school database has PUPIL (PupilID, Name, FormID) and FORM (FormID, Tutor). State what referential integrity does in each case.
- Adding a pupil with
FormID7B when no form7Bexists: refused, since the foreign key doesn't match any primary key inFORM. The form must be created first. - Deleting form
8Cwhile pupils are still in it: refused (restrict), or the pupils are deleted (cascade), or theirFormIDis cleared (set null), depending on the rule chosen. The sensible choice is to move the pupils to another form first. - Changing the
FormIDof a form from9Ato9X: the pupils in it would be orphaned, unless the DBMS cascades the update to theirFormID.
Without referential integrity, pupils could be left in forms that don't exist, and reports would be wrong.
Exam tip: define it in terms of foreign keys matching existing primary keys, and give an example of an action it prevents (adding a record that refers to a missing one, or deleting a record that others depend on). Know that the system can restrict or cascade.
Check yourself
What is referential integrity?
The rule that every foreign key value must match an existing primary key value in the table it refers to, so a record can't refer to something that doesn't exist.
What is an orphan record?
A record whose foreign key refers to a record that doesn't exist in the related table.
What happens if you try to delete a record that other records refer to, and the rule is "restrict"?
The deletion is refused until the related records are removed or changed.
What does "cascade delete" do?
When a record is deleted, all the records that refer to it are deleted too.