Skip to main content

How to Make a Foreign Key from Two Columns in SQLite

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

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​

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>)
);
PartWhat 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 --version to 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:

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;

The two_column_foreign_key.sql script in a macOS-style code window: PRAGMA foreign_keys = ON, a classes table with PRIMARY KEY (course, section), a seats table with FOREIGN KEY (course, section) REFERENCES classes (course, section), the classes Math 1 and Art 2, seats for Ana in Math 1 and Ben in Math 2, and a SELECT of every seat.

  • classes has PRIMARY 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:

Terminal
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.

Output
Error near line 19 of two_column_foreign_key.sql: FOREIGN KEY constraint failed
+---------+--------+---------+
| student | course | section |
+---------+--------+---------+
| Ana | Math | 1 |
+---------+--------+---------+

The output in a terminal: an error near line 19 of two_column_foreign_key.sql, FOREIGN KEY constraint failed, then the seats table with one row, Ana in Math section 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​

MistakeWhat happensDo this instead
A separate REFERENCES on each columnInserting a seat fails with foreign key mismatch - "seats" referencing "classes": neither column alone is the parent's keyWrite one FOREIGN KEY (course, section) line after the columns
A parent with a separate UNIQUE on each columnThe same foreign key mismatchGive the parent PRIMARY KEY (course, section) or UNIQUE (course, section)

Troubleshooting​

SymptomLikely causeFix
FOREIGN KEY constraint failedThat pair isn't in the parent tableInsert the class first, or fix the pair
foreign key mismatch - "seats" referencing "classes"The parent's two columns aren't its key togetherMake them the primary key, or add UNIQUE (course, section)
Parse error near line 3 of two_column_foreign_key.sql: table classes already existsThe script already ran on school.dbDelete 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​