SQL > Advanced SQL > AUTO_INCREMENT

AUTO_INCREMENT is used in MySQL to create a numerical primary key value for each additional row of data.

SQL AUTO_INCREMENT automatically generates a unique sequential integer for each new row inserted, making it ideal for primary key columns. By default it starts at 1 and increments by 1, but both values can be customized.

Syntax

The syntax for AUTO_INCREMENT is as follows:

CREATE TABLE TABLE_NAME
(PRIMARY_KEY_COLUMN INT NOT NULL AUTO_INCREMENT
...
PRIMARY KEY (PRIMARY_KEY_COLUMN));

Example

Assume we want to create a table that consists of a primary key, last name, and first name. We run the following CREATE TABLE statement:

CREATE TABLE USER_TABLE
(Userid int NOT NULL AUTO_INCREMENT,
 Last_Name varchar(50),
 First_Name varchar(50),
 PRIMARY KEY (Userid)); 

Upon creation, there is no data in this table.

We insert the first value:

INSERT INTO USER_TABLE VALUES ('Perry', 'Jonathan');

Now the table contains the following data:

Table USER_TABLE

 Userid  Last_Name  First_Name 
 1  Perry  Jonathan 

We then insert the second value:

INSERT INTO USER_TABLE VALUES ('Davis', 'Nancy');

Now the table has the following values:

Table USER_TABLE

 Userid  Last_Name  First_Name 
 1  Perry  Jonathan 
 2  Davis  Nancy 

Notice when we insert the first row, Userid is set to 1. When we insert the second row, Userid increases by 1 and becomes 2.

By default, AUTO_INCREMENT starts with 1 and increases by 1. To change the default starting value, we can use the ALTER TABLE command as follows:

ALTER TABLE TABLE_NAME AUTO_INCREMENT = [New Number];

where [New Number] is the starting value we want to use.

The AUTO INCREMENT interval value is controlled by the MySQL Server variable auto_increment_increment and applies globally. To change this to a number different from the default of 1, use the following command in MySQL:

mysql>
SET @@auto_increment_increment = [interval number];

where [interval number] is the interval value we want to use. So, if we want to set the interval to be 5, we would issue the following command:

mysql>
SET @@auto_increment_increment = 5; 

Frequently Asked Questions

What is AUTO_INCREMENT in MySQL?
AUTO_INCREMENT is a MySQL column attribute applied to integer primary key columns. It automatically generates a unique, sequential numeric value for each new row inserted — starting at 1 and incrementing by 1 by default — so you never need to manually assign primary key values.
How do I change the starting value of AUTO_INCREMENT?
Use the ALTER TABLE command: ALTER TABLE table_name AUTO_INCREMENT = [new_number]; This sets the next auto-generated ID to the specified number. Useful when you want IDs to start from a number other than 1.
How do I change the AUTO_INCREMENT interval in MySQL?
The interval is controlled by the MySQL server variable auto_increment_increment. Change it with: SET @@auto_increment_increment = [interval_number]; Note this applies globally to the MySQL server session.
Do I need to supply a value for an AUTO_INCREMENT column when inserting?
No. Simply omit the AUTO_INCREMENT column from your INSERT statement. MySQL automatically assigns the next sequential value, so you only need to supply values for the other columns.


Next: SQL IDENTITY

This page was last updated on March 19, 2026.




Copyright © 2026   1keydata.com   All Rights Reserved     Privacy Policy     About   Contact