Skip to main content

How to Add a Primary Key to a Table in SQLite

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
CREATE TABLE <table> (
<column> <type> NOT NULL PRIMARY KEY,
<other column> <type>
);
PartWhat it means
<column> <type>The column that identifies each row, such as student_id TEXT
PRIMARY KEYNo two rows may have the same value in this column
NOT NULLThe 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 --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 primary_key.sql:

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;

The primary_key.sql script in a macOS-style code window: a students table whose student_id column is TEXT NOT NULL PRIMARY KEY, three inserts for Ana as S-101, Ben as S-102 and Cleo as S-101 again, and a SELECT of every row.

  • student_id TEXT NOT NULL PRIMARY KEY makes student_id the key: required, and never repeated.
  • The three INSERT lines add Ana as S-101, Ben as S-102, then Cleo, again as S-101.
  • SELECT * FROM students shows the rows that made it into the table.

Step 2: Run It​

In the folder that holds the script, run:

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

Output
Error near line 8 of primary_key.sql: UNIQUE constraint failed: students.student_id
+------------+------+
| student_id | name |
+------------+------+
| S-101 | Ana |
| S-102 | Ben |
+------------+------+

The output in a terminal: an error near line 8 of primary_key.sql, UNIQUE constraint failed: students.student_id, then the students table with two rows, S-101 Ana and 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​

MistakeWhat happensDo this instead
Leaving out NOT NULL on a TEXT keyTwo students with a NULL student_id both went in. SQLite allows NULL in a primary key that isn't INTEGERWrite NOT NULL PRIMARY KEY
Writing PRIMARY KEY after two columnsParse error ...: table "students" has more than one primary keyFor a key made of two columns, write one PRIMARY KEY (<column 1>, <column 2>) line after the columns

Troubleshooting​

SymptomLikely causeFix
UNIQUE constraint failed: students.student_idAnother row already has that IDGive the new row an ID no other row has
Parse error near line 1 of primary_key.sql: table students already existsThe script already ran on school.dbDelete 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​