How to Make a Foreign Key from Two Columns in SQLite
A class here is a course plus a section: Math 1 and Art 2 exist, but Math 2 doesn't. To point at a class, a seat needs both values, and the check has to use them together. This guide builds a foreign key from two columns, with real output from SQLite 3.54.1.
Syntax
PRAGMA foreign_keys = ON;
CREATE TABLE <child table> (
<column 1> <type> NOT NULL,
<column 2> <type> NOT NULL,
FOREIGN KEY (<column 1>, <column 2>)
REFERENCES <parent table> (<parent column 1>, <parent column 2>)
);
| Part | What it means |
|---|---|
FOREIGN KEY (<column 1>, <column 2>) | A separate line after the columns: the child columns, checked as a pair |
REFERENCES <parent table> (<parent column 1>, <parent column 2>) | The parent's columns, in the same order. Together they must be its primary key, or have one UNIQUE constraint on both |
PRAGMA foreign_keys = ON; | Turns the check on. SQLite leaves it off unless you ask |
SQLite's documentation on composite foreign keys: "A composite foreign key constraint is one where the child and parent keys are both composite keys."
Prerequisites
- The sqlite3 command-line shell. This guide uses SQLite 3.54.1. Run
sqlite3 --versionto see yours; the SQLite download page has the command-line tools for Windows, macOS and Linux.
Step 1: Write the Script
Save this as two_column_foreign_key.sql:
PRAGMA foreign_keys = ON;
CREATE TABLE classes (
course TEXT NOT NULL,
section INTEGER NOT NULL,
PRIMARY KEY (course, section)
);
CREATE TABLE seats (
student TEXT NOT NULL,
course TEXT NOT NULL,
section INTEGER NOT NULL,
FOREIGN KEY (course, section) REFERENCES classes (course, section)
);
INSERT INTO classes (course, section) VALUES ('Math', 1);
INSERT INTO classes (course, section) VALUES ('Art', 2);
INSERT INTO seats (student, course, section) VALUES ('Ana', 'Math', 1);
INSERT INTO seats (student, course, section) VALUES ('Ben', 'Math', 2);
SELECT * FROM seats;

classeshasPRIMARY KEY (course, section), so a class is the pair.FOREIGN KEY (course, section) REFERENCES classes (course, section)checks each seat's pair against the classes.- Ana's seat is in Math 1, which exists. Ben's is in Math 2: Math is a course and 2 is a section, in Art, but there's no Math 2.
Step 2: Run It
In the folder that holds the script, run:
sqlite3 -table school.db ".read two_column_foreign_key.sql"
sqlite3 opens the database file school.db, and creates it when it isn't there. -table prints each result as a table, and .read runs the statements in the script. The command is the same in bash, zsh and PowerShell.
Error near line 19 of two_column_foreign_key.sql: FOREIGN KEY constraint failed
+---------+--------+---------+
| student | course | section |
+---------+--------+---------+
| Ana | Math | 1 |
+---------+--------+---------+

Ana's seat went in. Ben's was refused, although each of its values appears in classes on its own: the check compares the pair, and there's no Math 2.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
A separate REFERENCES on each column | Inserting a seat fails with foreign key mismatch - "seats" referencing "classes": neither column alone is the parent's key | Write one FOREIGN KEY (course, section) line after the columns |
A parent with a separate UNIQUE on each column | The same foreign key mismatch | Give the parent PRIMARY KEY (course, section) or UNIQUE (course, section) |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
FOREIGN KEY constraint failed | That pair isn't in the parent table | Insert the class first, or fix the pair |
foreign key mismatch - "seats" referencing "classes" | The parent's two columns aren't its key together | Make them the primary key, or add UNIQUE (course, section) |
Parse error near line 3 of two_column_foreign_key.sql: table classes already exists | The script already ran on school.db | Delete school.db and run the script again |
FAQ
What if one of the two values is NULL?
Then SQLite skips the check. With columns that allowed NULL, a seat in Physics with a NULL section went in, though there's no Physics class. NOT NULL on both columns, as in this guide, prevents that.
Can the parent use UNIQUE instead of a primary key?
Yes, one UNIQUE (course, section) on the pair. With it, Ana's seat in Math 1 went in, and Ben's in Math 2 was refused with FOREIGN KEY constraint failed.
Conclusion
FOREIGN KEY (course, section) REFERENCES classes (course, section) let Ana into Math 1 and refused Ben's Math 2, even though Math and 2 each exist on their own.
Additional Resources
- Composite foreign key constraints — foreign keys made of several columns.
- SQLite Foreign Key Support — parent and child keys, and when SQLite checks them.
