

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we change MySQL AUTO_INCREMENT starting number?
MySQL AUTO_INCREMENT value starts from 1 but we can change it with the help of following two ways −
With the help of ALTER TABLE query
We can use ALTER TABLE query to change the staring value of AUTO_INCREMENT as follows −
ALTER TABLE table_name AUTO_INCREMENT = value;
Example
Suppose we have created a table having column ‘id’ as AUTO_INCREMENT. Now if we will insert the values in it then the sequence number would start from 1 as you can see this in following queries −
mysql> Create Table EMP(id int NOT NULL PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR(10)); Query OK, 0 rows affected (0.07 sec) mysql> Insert Into EMP(Name) Values('Aryan'); Query OK, 1 row affected (0.02 sec) mysql> Insert Into EMP(Name) Values('Yash'); Query OK, 1 row affected (0.04 sec) mysql> Select * from EMP; +----+-------+ | id | NAME | +----+-------+ | 1 | Aryan | | 2 | Yash | +----+-------+ 2 rows in set (0.00 sec)
Now, if we want to change the sequence number afterward then we need to use ALTER TABLE query to change the value of AUTO_INCREMENT as follows −
mysql> Alter table emp auto_increment = 10; Query OK, 2 rows affected (0.25 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> Insert Into EMP(Name) Values('Daksh'); Query OK, 1 row affected (0.03 sec) mysql> Insert Into EMP(Name) Values('Shayra'); Query OK, 1 row affected (0.06 sec) mysql> Select * from EMP; +----+--------+ | id | NAME | +----+--------+ | 1 | Aryan | | 2 | Yash | | 10 | Daksh | | 11 | Shayra | +----+--------+ 4 rows in set (0.00 sec)
The query above has changed the value of AUTO_INCREMENT to 10 hence on inserting new values after that we will get the sequence number from 10 onwards.
With the help of CREATE TABLE query
We can also change the AUTO_INCREMENT value while creating the table. It can be done by specifying the value of AUTO_INCREMENT with CREATE TABLE query as follows −
CREATE TABLE (Column1 INT PRIMARY KEY NOT NULL AUTO_INCREMENT, Column2 data type) AUTO_INCREMENT = value;
Example
mysql> Create Table EMP1(id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, NAME VARCHAR(10)) AUTO_INCREMENT = 100; Query OK, 0 rows affected (0.11 sec)
The query above specified the value of AUTO_INCREMENT to 100 at the creation of the table. Now if we will insert the values in it then the sequence number would start from 100 instead of by default value 1 as follows −
mysql> Insert into emp1(name) values('Sohan'); Query OK, 1 row affected (0.04 sec) mysql> Insert into emp1(name) values('Harshit'); Query OK, 1 row affected (0.05 sec) mysql> Select * from emp1; +-----+---------+ | id | NAME | +-----+---------+ | 100 | Sohan | | 101 | Harshit | +-----+---------+ 2 rows in set (0.00 sec)
- Related Questions & Answers
- How to change auto increment number in MySQL?
- How to change auto increment number in the beginning in MySQL?
- Change the Auto Increment counter in MySQL?
- Passing NULL to MySQL for auto increment?
- How to make MySQL table primary key auto increment?
- Set custom Auto Increment with ZEROFILL in MySQL
- How can I set my auto-increment value to begin from 1 in MySQL?
- How to get the next auto-increment id in MySQL?
- How to set initial value and auto increment in MySQL?
- Display auto increment user id sequence number to begin from 001 in MySQL?
- How to handle fragmentation of auto increment ID column in MySQL?
- MySQL query to set my auto increment column ( id ) to zero or reset the value of auto increment field?
- Two columns as primary key with auto-increment in MySQL?
- How do I begin auto increment from a specific point in MySQL?
- How to add auto-increment to column in MySQL database using PhpMyAdmin?