
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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 Articles
- 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?
- How to make MySQL table primary key auto increment?
- How can I set my auto-increment value to begin from 1 in MySQL?
- Passing NULL to MySQL for auto increment?
- How to get the next auto-increment id in MySQL?
- How to set initial value and auto increment in MySQL?
- Set custom Auto Increment with ZEROFILL 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 - How can I fix an auto increment field with deleted rows from 1,2,3,4,5 to 1,3,5). Now we want it to be 1,2,3
- MySQL query to set my auto increment column ( id ) to zero or reset the value of auto increment field?
- How to auto-increment value of tables to lower value in MySQL?
- How to add auto-increment to column in MySQL database using PhpMyAdmin?
