
- 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 to change auto increment number in MySQL?
The auto_increment is a default property that automatically increments the newly added record by 1. The starting number can be changed with the help of the alter command.
First, a table is created with the help of the insert command. This is given as follows −
mysql> CREATE table AutoIncrementTable -> ( -> id int auto_increment, -> name varchar(200), -> Primary key(id) -> ); Query OK, 0 rows affected (0.70 sec)
After creating the table, records are inserted into the table with the help of the insert command This is given as follows −
mysql> INSERT into AutoIncrementTable(name) values('Carol'); Query OK, 1 row affected (0.19 sec) mysql> INSERT into AutoIncrementTable(name) values('Bob'); Query OK, 1 row affected (0.15 sec) mysql> INSERT into AutoIncrementTable(name) values('John'); Query OK, 1 row affected (0.18 sec)
Now, the records in the table can be seen with the help of the select command. This is given as follows −
mysql> SELECT * from AutoIncrementTable;
The output obtained from the above query is given below −
+----+-------+ | id | name | +----+-------+ | 1 | Carol | | 2 | Bob | | 3 | John | +----+-------+ 3 rows in set (0.00 sec)
Now, three records have been inserted in the table and the id is incremented by 1 each time. Now the Auto Increment is changed so that the id of the next record starts from 1000.
The syntax to change the auto_increment is given as follows.
alter table yourTableName auto_increment=startingNumber;
The above syntax is used to change the auto_increment by 1000. This is shown below −
mysql> alter table AutoIncrementTable auto_increment = 1000; Query OK, 0 rows affected (0.16 sec) Records: 0 Duplicates: 0 Warnings: 0
After successfully changing the auto_increment, more recors are inserted into the table. This is shown below −
mysql> INSERT into AutoIncrementTable(name) values('Taylor'); Query OK, 1 row affected (0.12 sec) mysql> INSERT into AutoIncrementTable(name) values('Sam'); Query OK, 1 row affected (0.17 sec)
Now, the table records are viewed using the select statement. It can be seen that the 4th record number starts from 1000.
mysql> SELECT * from AutoIncrementTable;
The following is the output
+------+--------+ | id | name | +------+--------+ | 1 | Carol | | 2 | Bob | | 3 | John | | 1000 | Taylor | | 1001 | Sam | +------+--------+ 5 rows in set (0.00 sec)
- Related Articles
- How to change auto increment number in the beginning in MySQL?
- Change the Auto Increment counter in MySQL?
- How to get the next auto-increment id in MySQL?
- How to set initial value and auto increment in MySQL?
- How to make MySQL table primary key auto increment?
- Passing NULL to MySQL for auto increment?
- Display auto increment user id sequence number to begin from 001 in MySQL?
- How to handle fragmentation of auto increment ID column in MySQL?
- How to add auto-increment to column in MySQL database using PhpMyAdmin?
- How to auto-increment value of tables to lower value in MySQL?
- Set custom Auto Increment with ZEROFILL in MySQL
- MySQL query to set my auto increment column ( id ) to zero or reset the value of auto increment field?
- How to create a table with auto-increment column in MySQL using JDBC?
- How do I begin auto increment from a specific point in MySQL?
- How can I set my auto-increment value to begin from 1 in MySQL?
