Auto Increment
Learn how the AUTO_INCREMENT attribute automatically generates unique, incrementing values for a column, typically the primary key.
Introduction
Every row usually needs a unique identifier, but manually tracking "what is the next free ID?" in your application code is tedious and error-prone, especially with multiple users inserting data at the same time.
MySQL solves this with the AUTO_INCREMENT attribute, which automatically generates the next unique number for a column every time a new row is inserted.
- What AUTO_INCREMENT does and where it is typically used.
- How to define an auto-incrementing primary key.
- What happens across multiple inserts.
- How to check the ID that was just generated with LAST_INSERT_ID().
- Why you should almost never assign IDs manually when using AUTO_INCREMENT.
What is AUTO_INCREMENT?
AUTO_INCREMENT is a column attribute that tells MySQL to automatically generate a new, unique, incrementing number for that column each time a row is inserted — you never have to supply a value for it yourself.
It is almost always used on a numeric PRIMARY KEY column, since that is exactly the kind of column that needs a guaranteed-unique value for every single row.
A table can have at most one AUTO_INCREMENT column, and that column must also be indexed — in practice, it is nearly always the primary key.
Defining an Auto-Increment Column
You add the AUTO_INCREMENT keyword right after the column's data type.
CREATE TABLE students (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
email VARCHAR(100)
);Now "id" will be filled in automatically by MySQL, starting at 1 by default and increasing by 1 with every new row.
Worked Example: Multiple Inserts
Notice that the "id" column is left out of the column list entirely — MySQL generates it for us.
INSERT INTO students (name, email) VALUES ('Alex', 'alex@example.com');
INSERT INTO students (name, email) VALUES ('Sam', 'sam@example.com');
INSERT INTO students (name, email) VALUES ('Priya', 'priya@example.com');
SELECT * FROM students;+----+-------+---------------------+
| id | name | email |
+----+-------+---------------------+
| 1 | Alex | alex@example.com |
| 2 | Sam | sam@example.com |
| 3 | Priya | priya@example.com |
+----+-------+---------------------+Each new row automatically received the next available number — 1, then 2, then 3 — without any of the INSERT statements mentioning "id" at all.
Checking the Last Inserted ID
After inserting a row, you often need to know which ID MySQL just generated — for example, to immediately insert related data in another table. The LAST_INSERT_ID() function returns exactly that, for the current connection.
INSERT INTO students (name, email) VALUES ('Rohan', 'rohan@example.com');
SELECT LAST_INSERT_ID();+------------------+
| LAST_INSERT_ID() |
+------------------+
| 4 |
+------------------+LAST_INSERT_ID() is scoped to your own connection/session — it will never return an ID generated by a different, simultaneous connection, which makes it safe to use even with many users inserting data at once.
Why You Rarely Assign IDs Manually
You could technically still specify an id value yourself in an INSERT, and MySQL will accept it as long as it does not collide with an existing one. But doing this regularly is risky.
INSERT INTO students (id, name, email) VALUES (10, 'Zara', 'zara@example.com');
INSERT INTO students (name, email) VALUES ('Kabir', 'kabir@example.com');
SELECT * FROM students WHERE name IN ('Zara', 'Kabir');+----+-------+---------------------+
| id | name | email |
+----+-------+---------------------+
| 10 | Zara | zara@example.com |
| 11 | Kabir | kabir@example.com |
+----+-------+---------------------+Notice MySQL jumped straight to 11 for the next auto-generated row, because it always continues counting from the highest value ever used, even one you supplied manually. Manually assigning IDs makes it easy to accidentally create gaps, collisions with concurrent inserts, or IDs that conflict with future auto-generated ones — so it is best left to MySQL.
Common Mistakes
- Manually specifying id values regularly, which can create unpredictable gaps or future collisions.
- Assuming AUTO_INCREMENT values are always perfectly sequential with no gaps — failed inserts and deleted rows can still leave gaps.
- Calling LAST_INSERT_ID() from a different connection than the one that performed the insert — it only reflects your own session.
- Forgetting that an auto-increment column must be indexed (it usually already is, as the primary key).
Key Takeaways
- AUTO_INCREMENT automatically generates a unique, incrementing value for a column on every insert.
- It is almost always applied to a numeric PRIMARY KEY column.
- Omitting the auto-increment column from an INSERT lets MySQL generate its value.
- LAST_INSERT_ID() returns the most recent auto-generated ID for your current connection.
- Manually assigning IDs is possible but discouraged, since it can create gaps or future conflicts.
Summary
AUTO_INCREMENT removes an entire class of bugs and complexity by letting MySQL generate unique row identifiers for you, instead of your application having to track and coordinate the "next" ID itself.
In this lesson, you defined an auto-incrementing primary key, inserted multiple rows without specifying an id, and checked the generated value with LAST_INSERT_ID(). Next, you'll take a deeper look at the INSERT statement itself.
- You understand what AUTO_INCREMENT does.
- You can define and use an auto-incrementing column.
- You know how to retrieve the last generated ID.
- You are ready to explore the INSERT statement in depth.