PRIMARY KEY in SQL
Give every row a unique, permanent identity
A PRIMARY KEY is the column (or column pair) that identifies each row uniquely. The engine enforces two rules forever: values are never NULL and never duplicated. It also builds an index on the key automatically, so lookups by id are fast. Every well-designed table has one — order numbers, user ids, employee ids all exist because the primary key guarantees them.
Declare the key
Inline (id INTEGER PRIMARY KEY) or as a table clause (PRIMARY KEY (id)) — both create the same constraint plus its supporting index.
Every INSERT is checked
The engine tests the new key value against the existing set before writing: duplicate or NULL means the whole statement fails with a constraint violation.
The index does double duty
The key’s B-tree keeps lookups by id at a handful of page reads regardless of table size — enforcement and speed from one structure.
Updates are checked too
Changing a key value re-runs the uniqueness test; most designs treat ids as immutable and never update them at all.
Composite keys span columns
PRIMARY KEY (student_id, course_id) makes the pair unique — either column alone may repeat, the combination may not.
| result |
|---|
1INSERT INTO employees (id, name, department, salary, age)2VALUES (1, 'Zara', 'Engineering', 80000, 30);
| error | |
|---|---|
| ERROR: duplicate key value violates unique constraint "employees_pkey" | Detail: Key (id)=(1) already exists. |
id 1 already belongs to Alice, so the engine refuses the insert with a constraint violation. The table is unchanged — that refusal is the whole point: an id can never quietly point at two people.
Unique id — accepted
A fresh key value sails through the check and lands in the table.
1INSERT INTO employees (id, name, department, salary, age)2VALUES (7, 'Grace', 'Engineering', 81000, 31);
| id | name | department | salary | age |
|---|---|---|---|---|
| 7 | Grace | Engineering | 81000 | 31 |
Duplicate id — rejected
The second row with id 1 cannot exist; the engine refuses the statement.
1INSERT INTO employees (id, name, department, salary, age)2VALUES (1, 'Zara', 'Engineering', 80000, 30);
| error |
|---|
| ERROR: duplicate key value violates unique constraint "employees_pkey" |
NULL id — rejected
Identity requires a value; NULL means “unknown”, which cannot identify a row.
1INSERT INTO employees (id, name, department, salary, age)2VALUES (NULL, 'Hugo', 'Sales', 49000, 27);
| error |
|---|
| ERROR: null value in column "id" violates not-null constraint |
Composite key pairs
Enrollment tables live on composite keys: the pair must be unique, the parts may repeat.
1CREATE TABLE enrollments (2 student_id INTEGER,3 course_id INTEGER,4 PRIMARY KEY (student_id, course_id)5);6-- (1, 100) ok · (1, 101) ok · (1, 100) again → duplicate
| pair | result |
|---|---|
| (1, 100) | OK |
| (1, 101) | OK |
| (1, 100) | ERROR: duplicate key |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.