Primary Key vs. Foreign Key With a SQL Example
A primary key uniquely identifies each row; a foreign key points to a primary key in another table. Six statements and what SQLite accepted or rejected.
A primary key is the column (or columns) that uniquely identifies each row in a table: no duplicates, no NULLs. A foreign key is a column in another table that refers to that primary key, so the database can refuse rows that point at something that doesn’t exist. Primary keys give rows an identity; foreign keys keep the links between tables valid (referential integrity).
Two tables
PRAGMA foreign_keys = ON; -- SQLite only: enforcement is off by default
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id), -- foreign key
total REAL NOT NULL
);
INSERT INTO customers VALUES (1, 'Ada'), (2, 'Grace'), (3, 'Linus');
INSERT INTO orders VALUES (101, 1, 25.00), (102, 1, 9.50);
customers.id is the primary key. orders.customer_id is a foreign key: each order must belong to a customer that exists.
What the database accepted and rejected
We ran six statements against those tables in SQLite 3.51.0. Pick one to see the recorded result:
Rejected
Error: FOREIGN KEY constraint failed (SQLite result code 19)There is no customers row with id 9.
The verbatim shell output for the failures was:
-- order for customer 9 (does not exist)
Runtime error near line 9: FOREIGN KEY constraint failed (19)
-- duplicate primary key
Runtime error near line 11: UNIQUE constraint failed: customers.id (19)
-- delete a customer who has orders
Runtime error near line 13: FOREIGN KEY constraint failed (19)
And the orders table at the end, joined to customers:
┌─────┬─────────────┬───────┬───────┐
│ id │ customer_id │ name │ total │
├─────┼─────────────┼───────┼───────┤
│ 101 │ 1 │ Ada │ 25.0 │
│ 102 │ 1 │ Ada │ 9.5 │
│ 103 │ 2 │ Grace │ 18.0 │
│ 105 │ │ │ 7.0 │
└─────┴─────────────┴───────┴───────┘
Order 105 was accepted with a NULL foreign key: a foreign key only checks values that are present. Add NOT NULL to the column if every order must have a customer.
Comparison
| Primary key | Foreign key | |
|---|---|---|
| Purpose | identify each row | link a row to a row in another (or the same) table |
| Uniqueness | must be unique | can repeat (many orders per customer) |
| NULL allowed | no (by standard SQL) | yes, unless declared NOT NULL |
| Per table | one (possibly multi-column) | any number |
| Enforced on | insert and update of the key | insert/update of the child, delete/update of the parent |
| Index | created automatically | usually worth adding yourself |
Deleting a parent row
Deleting customer 1, who still had orders, failed. That is the default NO ACTION behaviour. You can choose otherwise in the definition:
ON DELETE CASCADEdeletes the customer’s orders too.ON DELETE SET NULLkeeps the orders and clearscustomer_id.ON DELETE RESTRICTrefuses immediately, like our result.
SQLite’s off-by-default trap
SQLite only enforces foreign keys after PRAGMA foreign_keys = ON, and the setting is per connection. With it off, the same insert of an order for customer 9 succeeded in our test (rows_inserted = 1), leaving an orphaned row. PostgreSQL, MySQL (InnoDB) and SQL Server enforce foreign keys by default.
The same two tables are used to demonstrate combining result sets in UNION vs. UNION ALL and NULL comparisons in SQL not equal.
Sources
- SQLite Foreign Key Support — SQLite
- CREATE TABLE (PRIMARY KEY, UNIQUE) — SQLite
How we made this
Reproduced in a local lab. We ran the schema and statements below in the sqlite3 shell (SQLite 3.51.0) with PRAGMA foreign_keys = ON, then repeated one insert with foreign keys off. Errors and the final table are quoted verbatim. The interactive example replays those recorded results.
Performed by Claude, an AI assistant working in the BinConvert repository (SQLite 3.51.0 (sqlite3 shell on macOS)), November 16, 2026.
This article was drafted with AI assistance. Every example, calculation and command output on this page was re-run or checked before release; the session was re-run in a fresh database and every message compared. Review: automated test suite and source check, November 16, 2026.
Spotted an error? Tell us and we will correct it.