Skip to main content

How to Make a Primary Key from Two Columns in SQLite

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
CREATE TABLE <table> (
<column 1> <type> NOT NULL,
<column 2> <type> NOT NULL,
PRIMARY KEY (<column 1>, <column 2>)
);
PartWhat 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 NULLBoth 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 --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_key.sql:

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;

The two_column_key.sql script in a macOS-style code window: an enrollments table with student_id and course columns and PRIMARY KEY (student_id, course), four inserts where S-101 joins Math, Art, S-102 joins Math, then S-101 joins Math again, and a SELECT of every row.

  • PRIMARY KEY (student_id, course) makes the pair the key. Either value alone may repeat.
  • S-101 joins Math and Art: same student, different courses.
  • S-102 joins Math: same course, different student.
  • The last INSERT repeats S-101 in Math, a pair that's already there.

Step 2: Run It​

In the folder that holds the script, run:

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

Output
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 output in a terminal: an error near line 10 of two_column_key.sql, UNIQUE constraint failed: enrollments.student_id, enrollments.course, then three enrollments: S-101 in Math, S-101 in Art and S-102 in 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​

MistakeWhat happensDo this instead
Writing PRIMARY KEY after each columnParse error ...: table "enrollments" has more than one primary keyWrite one PRIMARY KEY (student_id, course) line after the columns
Leaving out NOT NULLThe key columns accept NULL, because SQLite allows it in a key that isn't INTEGERMark both columns NOT NULL

Troubleshooting​

SymptomLikely causeFix
UNIQUE constraint failed: enrollments.student_id, enrollments.courseThat student is already in that courseSkip the row, or change one of the two values
Parse error near line 1 of two_column_key.sql: table enrollments already existsThe script already ran on school.dbDelete 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​