MySQL AUTO_INCREMENT
AUTO_INCREMENT makes MySQL generate a new, unique number for a column every time a row is inserted without a value for it. It is the standard way to create surrogate primary keys: customer_id, order_id, product_id in the shop database all use it.
CREATE TABLE table_name (
id INT AUTO_INCREMENT PRIMARY KEY,
...
);Rules: a table can have only one AUTO_INCREMENT column, it must be indexed (normally it is the primary key), and it should be an integer type. Use INT UNSIGNED (up to about 4.29 billion) or BIGINT UNSIGNED for tables that may grow very large.
Basic use
CREATE TABLE tags (
tag_id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
label VARCHAR(20) NOT NULL
);
INSERT INTO tags (label) VALUES ('organic'), ('vegan'), ('local');
INSERT INTO tags (tag_id, label) VALUES (NULL, 'fair-trade'); -- NULL also means "generate"
SELECT * FROM tags;
SELECT LAST_INSERT_ID() AS last_id;+--------+------------+
| tag_id | label |
+--------+------------+
| 1 | organic |
| 2 | vegan |
| 3 | local |
| 4 | fair-trade |
+--------+------------+
+---------+
| last_id |
+---------+
| 4 |
+---------+
Leaving the column out, or inserting NULL (or 0, by default), both generate the next value.
Numbers are never reused
The counter only moves forward. Deleting rows leaves gaps; the numbers are not handed out again:
CREATE TABLE tickets (id INT AUTO_INCREMENT PRIMARY KEY, txt VARCHAR(10));
INSERT INTO tickets (txt) VALUES ('a'), ('b'), ('c');
DELETE FROM tickets WHERE id = 3;
INSERT INTO tickets (txt) VALUES ('d');
SELECT * FROM tickets;+----+------+
| id | txt |
+----+------+
| 1 | a |
| 2 | b |
| 4 | d |
+----+------+
Gaps also appear after rolled-back transactions and failed inserts. This is by design: handing out numbers without coordination is what makes AUTO_INCREMENT fast when many clients insert at once. Never treat ids as a gap-free sequence (invoice numbers that must be consecutive need their own mechanism).
Note: Before MySQL 8.0 the counter lived only in memory, so after a restart it was reset to
MAX(id) + 1and deleted top numbers could be reused. MySQL 8 stores the counter persistently.
Explicit values and the starting point
You can insert an explicit id. If it is higher than the counter, the counter jumps past it:
CREATE TABLE invoices (invoice_no INT AUTO_INCREMENT PRIMARY KEY, amount DECIMAL(8,2));
INSERT INTO invoices (amount) VALUES (10.00);
INSERT INTO invoices (invoice_no, amount) VALUES (500, 20.00);
INSERT INTO invoices (amount) VALUES (30.00);
SELECT * FROM invoices;+------------+--------+
| invoice_no | amount |
+------------+--------+
| 1 | 10.00 |
| 500 | 20.00 |
| 501 | 30.00 |
+------------+--------+
To start numbering at a different value, set the table option AUTO_INCREMENT, either when creating the table or later:
CREATE TABLE members (
member_no INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(20)
) AUTO_INCREMENT = 1000;
INSERT INTO members (name) VALUES ('Ada'), ('Grace');
ALTER TABLE members AUTO_INCREMENT = 5000;
INSERT INTO members (name) VALUES ('Linus');
SELECT * FROM members;+-----------+-------+
| member_no | name |
+-----------+-------+
| 1000 | Ada |
| 1001 | Grace |
| 5000 | Linus |
+-----------+-------+
ALTER TABLE ... AUTO_INCREMENT can't move the counter below the current largest value; MySQL silently uses MAX + 1 instead.
LAST_INSERT_ID()
LAST_INSERT_ID() returns the most recent value generated on your connection. Other clients inserting at the same moment don't affect it, so it's the safe way to get the id of a row you just inserted (never use SELECT MAX(id) for that):
INSERT INTO customers (first_name, last_name, country, signup_date)
VALUES ('Paula', 'Nowak', 'Poland', '2025-06-10');
SELECT LAST_INSERT_ID() AS new_customer_id;
SELECT customer_id, first_name, last_name FROM customers WHERE customer_id = LAST_INSERT_ID();+-----------------+
| new_customer_id |
+-----------------+
| 16 |
+-----------------+
+-------------+------------+-----------+
| customer_id | first_name | last_name |
+-------------+------------+-----------+
| 16 | Paula | Nowak |
+-------------+------------+-----------+
Application drivers expose the same value directly, e.g. cursor.lastrowid in Python or insertId in Node's mysql2.
Test yourself
Rows with id 1, 2, 3 exist; row 3 is deleted. What id does the next insert get?
- 3
- 4
- 1
- It depends on the time
How do you make a new table start numbering at 1000?
- START WITH 1000
- CREATE TABLE ... (...) AUTO_INCREMENT = 1000
- INSERT id 999 and delete it
- SET @@auto_increment = 1000
Why is LAST_INSERT_ID() better than SELECT MAX(id) after an insert?
- It is shorter
- It is per connection, so other clients' inserts can't interfere
- MAX doesn't work on keys
- It also resets the counter