Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
MySQL + SQL · Lesson 41

AUTO_INCREMENT in MySQL

In this lesson: Auto-generate IDs in MySQL with AUTO_INCREMENT — set start value, reset counter and common errors — with output. In Hindi & English.

What is AUTO_INCREMENT

AUTO_INCREMENT tells MySQL to automatically generate a new, unique number for a column each time you insert a row. It is almost always used on the primary key, so you never have to think up ids yourself — MySQL fills them in, one higher each time.

💡 AUTO_INCREMENT = MySQL assigns the next id for you, so ids are unique automatically.

Creating an auto id

CREATE TABLE students (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(50)
);

Adding AUTO_INCREMENT to the integer primary key id means MySQL will number each new row automatically, starting from 1.

Inserting rows

INSERT INTO students (name) VALUES ('Aman');
INSERT INTO students (name) VALUES ('Diya');
SELECT * FROM students;
+----+------+ | id | name | +----+------+ | 1 | Aman | | 2 | Diya | +----+------+

Notice we did not give an id — MySQL assigned 1, then 2 automatically. You just insert the other columns.

Getting the last id

INSERT INTO students (name) VALUES ('Kabir');
SELECT LAST_INSERT_ID();
3

LAST_INSERT_ID() returns the id MySQL just generated. This is very useful when you insert a parent row and need its new id to insert related child rows.

Resetting the counter

ALTER TABLE students AUTO_INCREMENT = 100;
-- next inserted row gets id 100

You can set where the counter continues from. TRUNCATE TABLE students; also resets it back to 1, while DELETE leaves the counter where it was. You cannot set it below the highest existing id.

Useful notes

  • Gaps are normal. Deleting a row does not reuse its id; the counter keeps rising.
  • One per table. A table can have only one AUTO_INCREMENT column.
  • Must be a key. The column should be a primary or unique key.

These behaviours keep ids unique and predictable.

Common mistakes

  • Supplying the id yourself. Let MySQL generate it; just insert the other columns.
  • Expecting deleted ids to be reused. They are not — gaps are fine.
  • Setting AUTO_INCREMENT below the max id. MySQL ignores values that would clash.
  • Using it on a non-key column. AUTO_INCREMENT needs a primary or unique key.

Summary

  • AUTO_INCREMENT auto-generates unique ids for new rows.
  • Add it to an integer primary key: id INT PRIMARY KEY AUTO_INCREMENT.
  • Insert without the id; MySQL assigns the next number.
  • LAST_INSERT_ID() returns the id just created.
  • TRUNCATE resets the counter; deleted ids are not reused.

Next, add many rows at once with inserting multiple rows, or key concepts in primary key vs foreign key.

Frequently Asked Questions

What is AUTO_INCREMENT in MySQL?
AUTO_INCREMENT is a column setting that automatically generates a new, unique number for each inserted row. It is usually used on the primary key so you do not have to supply the id yourself — MySQL fills it in, increasing by one each time.
How do I use AUTO_INCREMENT?
Add AUTO_INCREMENT to an integer primary key column when creating the table, like id INT PRIMARY KEY AUTO_INCREMENT. Then insert rows without giving the id, and MySQL assigns the next number automatically.
How do I get the last inserted id in MySQL?
Use the LAST_INSERT_ID() function right after an INSERT: SELECT LAST_INSERT_ID();. It returns the auto-generated id of the most recent insert on your connection, which is useful for linking related rows.
How do I reset the AUTO_INCREMENT value?
Use ALTER TABLE tablename AUTO_INCREMENT = 1; to set the next value. Note that TRUNCATE also resets the counter, while DELETE does not. You cannot set it below the current maximum id in the table.
Does deleting rows reuse AUTO_INCREMENT numbers?
No. By default, deleted ids are not reused — the counter keeps going up. So if you delete row 5, the next insert may still be 6, leaving a gap. This is normal and keeps ids unique over time.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicComments in SQL Your progress: 41 / 151 (27%) · Lesson 42 / 151 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free