How to Make a Primary Key from Two Columns in SQLite
Some rows are identified by two values together. A student can take many courses, and a course has many students, but the same student can't enroll in the same course twice. A primary key made of both columns says exactly that. This guide builds one, with real output from SQLite 3.54.1.
Syntax
CREATE TABLE <table> (
<column 1> <type> NOT NULL,
<column 2> <type> NOT NULL,
PRIMARY KEY (<column 1>, <column 2>)
);
| Part | What it means |
|---|---|
<column 1>, <column 2> | The two columns, defined as usual |
PRIMARY KEY (<column 1>, <column 2>) | A separate line after the columns: the pair of values must be unique |
NOT NULL | Both columns must have a value. SQLite allows NULL in a key that isn't INTEGER unless you say so |
SQLite's documentation: "if a PRIMARY KEY clause is specified as a table-constraint, then the primary key of the table consists of the list of columns specified as part of the PRIMARY KEY clause."
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_key.sql:
CREATE TABLE enrollments (
student_id TEXT NOT NULL,
course TEXT NOT NULL,
PRIMARY KEY (student_id, course)
);
INSERT INTO enrollments (student_id, course) VALUES ('S-101', 'Math');
INSERT INTO enrollments (student_id, course) VALUES ('S-101', 'Art');
INSERT INTO enrollments (student_id, course) VALUES ('S-102', 'Math');
INSERT INTO enrollments (student_id, course) VALUES ('S-101', 'Math');
SELECT * FROM enrollments;

PRIMARY KEY (student_id, course)makes the pair the key. Either value alone may repeat.S-101joins Math and Art: same student, different courses.S-102joins Math: same course, different student.- The last
INSERTrepeatsS-101in Math, a pair that's already there.
Step 2: Run It
In the folder that holds the script, run:
sqlite3 -table school.db ".read two_column_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 10 of two_column_key.sql: UNIQUE constraint failed: enrollments.student_id, enrollments.course
+------------+--------+
| student_id | course |
+------------+--------+
| S-101 | Math |
| S-101 | Art |
| S-102 | Math |
+------------+--------+

The first three rows went in, even though S-101 and Math each appear twice: no pair repeated. Line 10, S-101 in Math again, did repeat a pair, and the error names both columns of the key. The shell reported it and carried on to the SELECT.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Writing PRIMARY KEY after each column | Parse error ...: table "enrollments" has more than one primary key | Write one PRIMARY KEY (student_id, course) line after the columns |
Leaving out NOT NULL | The key columns accept NULL, because SQLite allows it in a key that isn't INTEGER | Mark both columns NOT NULL |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
UNIQUE constraint failed: enrollments.student_id, enrollments.course | That student is already in that course | Skip the row, or change one of the two values |
Parse error near line 1 of two_column_key.sql: table enrollments already exists | The script already ran on school.db | Delete school.db and run the script again |
FAQ
Can a primary key have more than two columns?
Yes. List every column in the one clause, such as PRIMARY KEY (student_id, course, term). Then the three values together must be unique.
What's the difference from a UNIQUE constraint on the two columns?
Both stop a repeated pair. A table can have only one primary key, but any number of UNIQUE constraints. Another table can point at either with a foreign key.
Conclusion
PRIMARY KEY (student_id, course) let the same student and the same course appear on several rows, and stopped only the repeated pair: S-101 in Math.
Additional Resources
- The PRIMARY KEY — one-column and multi-column keys.
- CREATE TABLE — column and table constraints.
