How to Add a Primary Key to a Table in SQLite
A primary key is the column that tells rows apart: a student ID, an order number, a product code. SQLite makes sure no two rows share it. This guide keys a students table on student_id, then tries to add a second student with the same ID, with real output from SQLite 3.54.1.
Syntax
CREATE TABLE <table> (
<column> <type> NOT NULL PRIMARY KEY,
<other column> <type>
);
| Part | What it means |
|---|---|
<column> <type> | The column that identifies each row, such as student_id TEXT |
PRIMARY KEY | No two rows may have the same value in this column |
NOT NULL | The column must have a value. SQLite needs this on a key that isn't INTEGER, as Common Mistakes shows |
SQLite's documentation on primary keys: "Each row in a table with a primary key must have a unique combination of values in its primary key columns."
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 primary_key.sql:
CREATE TABLE students (
student_id TEXT NOT NULL PRIMARY KEY,
name TEXT NOT NULL
);
INSERT INTO students (student_id, name) VALUES ('S-101', 'Ana');
INSERT INTO students (student_id, name) VALUES ('S-102', 'Ben');
INSERT INTO students (student_id, name) VALUES ('S-101', 'Cleo');
SELECT * FROM students;

student_id TEXT NOT NULL PRIMARY KEYmakesstudent_idthe key: required, and never repeated.- The three
INSERTlines add Ana asS-101, Ben asS-102, then Cleo, again asS-101. SELECT * FROM studentsshows the rows that made it into the table.
Step 2: Run It
In the folder that holds the script, run:
sqlite3 -table school.db ".read primary_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 8 of primary_key.sql: UNIQUE constraint failed: students.student_id
+------------+------+
| student_id | name |
+------------+------+
| S-101 | Ana |
| S-102 | Ben |
+------------+------+

Line 8 tried to add Cleo as S-101, which Ana already has, so SQLite refused that row: UNIQUE constraint failed: students.student_id. The shell reported the error and carried on, so the SELECT shows the two rows that went in.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Leaving out NOT NULL on a TEXT key | Two students with a NULL student_id both went in. SQLite allows NULL in a primary key that isn't INTEGER | Write NOT NULL PRIMARY KEY |
Writing PRIMARY KEY after two columns | Parse error ...: table "students" has more than one primary key | For a key made of two columns, write one PRIMARY KEY (<column 1>, <column 2>) line after the columns |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
UNIQUE constraint failed: students.student_id | Another row already has that ID | Give the new row an ID no other row has |
Parse error near line 1 of primary_key.sql: table students already exists | The script already ran on school.db | Delete school.db and run the script again |
FAQ
Why does a repeated key say UNIQUE, not PRIMARY KEY?
SQLite checks a primary key with a unique index. Its documentation: "In most cases, UNIQUE and PRIMARY KEY constraints are implemented by creating a unique index in the database."
Why does SQLite allow NULL in a primary key?
It's an old bug SQLite keeps for compatibility. The documentation: "According to the SQL standard, PRIMARY KEY should always imply NOT NULL. Unfortunately, due to a bug in some early versions, this is not the case in SQLite." INTEGER PRIMARY KEY columns and STRICT tables don't have the problem.
Should my key be a number?
If you want SQLite to number the rows for you, use INTEGER PRIMARY KEY. A code people already use, such as a student ID, works well as a TEXT key.
Conclusion
With student_id TEXT NOT NULL PRIMARY KEY, SQLite kept Ana's S-101 and turned Cleo away with UNIQUE constraint failed: students.student_id, so every row has its own ID.
Additional Resources
- The PRIMARY KEY — how SQLite enforces primary keys, NULLs included.
- CREATE TABLE — the full syntax.
