Full answer
SQLite only enforces foreign keys when PRAGMA foreign_keys = ON is set on the connection. If an application forgot it, deletes may have left children pointing at missing parents.
CREATE TABLE students (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
city TEXT,
year INTEGER
);
CREATE TABLE courses (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
credits INTEGER
);
CREATE TABLE enrolments (
student_id INTEGER REFERENCES students(id),
course_id INTEGER REFERENCES courses(id),
marks INTEGER,
PRIMARY KEY (student_id, course_id)
);
INSERT INTO students VALUES (1,'Asha','Pune',1),(2,'Ben','Leeds',2),(3,'Chen','Pune',1),(4,'Dara',NULL,3),(5,'Eli','Leeds',2);
INSERT INTO courses VALUES (10,'Databases',4),(20,'Algorithms',4),(30,'Statistics',3),(40,'Ethics',2);
INSERT INTO enrolments VALUES
(1,10,88),(1,20,92),(2,10,75),(2,30,NULL),
(3,10,64),(3,20,71),(3,30,80),(5,20,55);Here a student is deleted while enforcement is off, so the delete succeeds and leaves enrolments behind:
DELETE FROM students WHERE id = 5;
SELECT COUNT(*) AS students_left FROM students;| students_left |
|---|
| 4 |
An anti-join finds them: keep enrolments with no matching student.
SELECT e.student_id, e.course_id
FROM enrolments e
LEFT JOIN students s ON s.id = e.student_id
WHERE s.id IS NULL
ORDER BY e.student_id, e.course_id;| student_id | course_id |
|---|---|
| 5 | 20 |
The NOT EXISTS form says the same thing and is easy to adapt into a DELETE or an UPDATE:
SELECT e.student_id, e.course_id
FROM enrolments e
WHERE NOT EXISTS (SELECT 1 FROM students s WHERE s.id = e.student_id)
ORDER BY e.student_id, e.course_id;| student_id | course_id |
|---|---|
| 5 | 20 |
SQLite also has a built-in checker that reports every row violating a declared foreign key:
SELECT "table", rowid, parent FROM pragma_foreign_key_check('enrolments') ORDER BY rowid;| table | rowid | parent |
|---|---|---|
| enrolments | 8 | students |
After cleaning up, turn enforcement on in every connection (or move to an engine that always enforces it), otherwise the orphans return. Decide what cleanup means before deleting: orphaned enrolments might need to be archived for audit rather than removed.
CREATE TABLE students (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
city TEXT,
year INTEGER
);
CREATE TABLE courses (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
credits INTEGER
);
CREATE TABLE enrolments (
student_id INTEGER REFERENCES students(id),
course_id INTEGER REFERENCES courses(id),
marks INTEGER,
PRIMARY KEY (student_id, course_id)
);
INSERT INTO students VALUES (1,'Asha','Pune',1),(2,'Ben','Leeds',2),(3,'Chen','Pune',1),(4,'Dara',NULL,3),(5,'Eli','Leeds',2);
INSERT INTO courses VALUES (10,'Databases',4),(20,'Algorithms',4),(30,'Statistics',3),(40,'Ethics',2);
INSERT INTO enrolments VALUES
(1,10,88),(1,20,92),(2,10,75),(2,30,NULL),
(3,10,64),(3,20,71),(3,30,80),(5,20,55);
DELETE FROM students WHERE id = 5;
SELECT COUNT(*) AS students_left FROM students;
SELECT e.student_id, e.course_id
FROM enrolments e
LEFT JOIN students s ON s.id = e.student_id
WHERE s.id IS NULL
ORDER BY e.student_id, e.course_id;
SELECT e.student_id, e.course_id
FROM enrolments e
WHERE NOT EXISTS (SELECT 1 FROM students s WHERE s.id = e.student_id)
ORDER BY e.student_id, e.course_id;
SELECT "table", rowid, parent FROM pragma_foreign_key_check('enrolments') ORDER BY rowid;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- Orphans are child rows whose parent key has no match
- LEFT JOIN ... WHERE parent.key IS NULL or NOT EXISTS finds them
- SQLite's PRAGMA foreign_key_check reports violations
- Enable enforcement after cleanup so orphans do not return
Common mistakes
- Counting NULL foreign keys as orphans when NULL is allowed
- Deleting orphans without checking whether they must be kept for audit
Follow-ups
- How would you delete the orphans in one statement?
- What ON DELETE options could have prevented this?
Tips
- Mention the SQLite pragma; it shows you know where the gap came from