SQL PRIMARY KEY
The primary key is the column (or set of columns) that identifies each row uniquely. It cannot be NULL, cannot repeat, and is what other tables point to with foreign keys. Every table should have one, and choosing it well is one of the first decisions in database design.
- Define single-column and composite primary keys
- Understand the errors a primary key prevents
- Choose between natural and surrogate keys
- Add, drop and inspect a primary key
Define a primary key
CREATE TABLE books ( book_id INT PRIMARY KEY, isbn CHAR(13) NOT NULL UNIQUE, title VARCHAR(100) NOT NULL);INSERT INTO books VALUES (1, '9780134685991', 'Effective Java'), (2, '9781492056355', 'Learning SQL');SELECT * FROM books;| book_id | isbn | title |
|---|---|---|
| 1 | 9780134685991 | Effective Java |
| 2 | 9781492056355 | Learning SQL |
book_id INT PRIMARY KEYmakesbook_idunique and NOT NULL.isbnis also unique, but it is a business value; the short integerbook_idis the key other tables will reference.
What a primary key prevents
CREATE TABLE books (book_id INT PRIMARY KEY, title VARCHAR(100) NOT NULL);INSERT INTO books VALUES (1, 'Effective Java');INSERT INTO books VALUES (1, 'Learning SQL');ERROR 1062 (23000): Duplicate entry '1' for key 'books.PRIMARY'
CREATE TABLE books (book_id INT PRIMARY KEY, title VARCHAR(100) NOT NULL);INSERT INTO books VALUES (NULL, 'Learning SQL');ERROR 1048 (23000): Column 'book_id' cannot be null
- Error 1062: a second row with
book_id = 1is a duplicate entry for keyPRIMARY. - Error 1048: the key cannot be NULL.
A table can have only one primary key:
CREATE TABLE t (a INT PRIMARY KEY, b INT PRIMARY KEY);ERROR 1068 (42000): Multiple primary key defined
Composite primary key
When no single column is unique, the key can be a combination. In order_items, an order has many products and a product appears in many orders, but each (order_id, product_id) pair appears once:
SHOW KEYS FROM order_items WHERE Key_name = 'PRIMARY';| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| order_items | 0 | PRIMARY | 1 | order_id | A | 1 | NULL | NULL | BTREE | YES | NULL | |||
| order_items | 0 | PRIMARY | 2 | product_id | A | 1 | NULL | NULL | BTREE | YES | NULL |
INSERT INTO order_items (order_id, product_id, quantity) VALUES (1001, 2, 3);ERROR 1062 (23000): Duplicate entry '1001-2' for key 'order_items.PRIMARY'
SHOW KEYSlists the two columns of the primary key in order (Seq_in_index1 and 2).- Order 1001 already contains product 2, so the same pair is rejected; to change the quantity,
UPDATEthe existing line.
Natural vs surrogate keys
| Natural key | Surrogate key | |
|---|---|---|
| Example | email, ISBN, PAN number | customer_id INT AUTO_INCREMENT |
| Meaning | real-world value | none, just a number |
| Can change? | yes (emails change) | never |
| Size | often long text | 4 or 8 bytes |
Most designs use a surrogate primary key (often AUTO_INCREMENT) and protect the natural identifier with a UNIQUE constraint, as the books table above does with isbn.
In InnoDB, rows are physically stored in primary-key order and every secondary index stores the primary key. A short, ever-increasing key (such as an auto-increment integer) keeps tables and indexes compact and inserts fast.
Add or drop a primary key
CREATE TABLE visits (visitor VARCHAR(30), visit_date DATE, pages INT);INSERT INTO visits VALUES ('asha', '2026-01-01', 3), ('asha', '2026-01-02', 5);ALTER TABLE visits ADD PRIMARY KEY (visitor, visit_date);SHOW KEYS FROM visits WHERE Key_name = 'PRIMARY';| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| visits | 0 | PRIMARY | 1 | visitor | A | 1 | NULL | NULL | BTREE | YES | NULL | |||
| visits | 0 | PRIMARY | 2 | visit_date | A | 2 | NULL | NULL | BTREE | YES | NULL |
- The table is created without a key.
ALTER TABLE ... ADD PRIMARY KEYadds a composite key; it works because the existing rows are unique and not NULL.ALTER TABLE visits DROP PRIMARY KEY;would remove it again.
Quick quiz
1. How many primary keys can a table have?
A table has exactly one primary key.
2. Can a primary key column contain NULL?
Primary key columns are always NOT NULL.
3. What is a surrogate key?
It has no business meaning, so it never needs to change.
Primary key FAQ
What is a primary key in SQL?
A column or group of columns whose values uniquely identify each row. It must be unique and cannot be NULL.
What is a composite primary key?
A primary key made of two or more columns, where only the combination must be unique, e.g. (order_id, product_id).
What is the difference between primary key and unique key?
A table has one primary key and it cannot contain NULL; it can have many unique keys, which may contain NULLs.
Should every table have a primary key?
Yes. Without one, rows cannot be identified reliably, and InnoDB creates a hidden key that you cannot use.