
- 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 do I begin auto increment from a specific point in MySQL?
To begin auto increment from a specific point, use ALTER command. The syntax is as follows −
ALTER TABLE yourTableName auto_increment = anySpecificPoint;
To understand the above concept, let us create a table. The query to create a table is as follows −
mysql> create table AutoIncrementSpecificPoint −> ( −> BookId int auto_increment not null, −> Primary key(BookId) −> ); Query OK, 0 rows affected (0.56 sec)
Now you can insert records using insert command.
The query is as follows −
mysql> insert into AutoIncrementSpecificPoint values(); Query OK, 1 row affected (0.17 sec) mysql> insert into AutoIncrementSpecificPoint values(); Query OK, 1 row affected (0.17 sec) mysql> insert into AutoIncrementSpecificPoint values(); Query OK, 1 row affected (0.09 sec) mysql> insert into AutoIncrementSpecificPoint values(); Query OK, 1 row affected (0.10 sec)
The above insert command begins the value from 1 and adds 1 to the next value. Now you can check all records from table using select statement.
The query is as follows −
mysql> select *from AutoIncrementSpecificPoint;
The following is the output −
+--------+ | BookId | +--------+ | 1 | | 2 | | 3 | | 4 | +--------+ 4 rows in set (0.00 sec)
Look at the above sample output, the auto_increment starts from 1.
Now to change the auto_increment to begin from a specific point, you can use ALTER command. The query is as follows −
mysql> alter table AutoIncrementSpecificPoint auto_increment = 100; Query OK, 0 rows affected (0.25 sec) Records: 0 Duplicates: 0 Warnings: 0
In the above query I have set the auto increment to 100. Let us now insert records once again in the table using insert command. The query is as follows −
mysql> insert into AutoIncrementSpecificPoint values(); Query OK, 1 row affected (0.25 sec) mysql> insert into AutoIncrementSpecificPoint values(); Query OK, 1 row affected (0.18 sec) mysql> insert into AutoIncrementSpecificPoint values(); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from AutoIncrementSpecificPoint;
The following is the output displaying the other values set for auto increment i.e. beginning from 100 −
+--------+ | BookId | +--------+ | 1 | | 2 | | 3 | | 4 | | 100 | | 101 | | 102 | +--------+ 7 rows in set (0.00 sec)
- Related Articles
- How can I set my auto-increment value to begin from 1 in MySQL?
- Display auto increment user id sequence number to begin from 001 in MySQL?
- How to change auto increment number in MySQL?
- How to auto increment with 1 after deleting data from a MySQL table?
- How do I exclude a specific record in MySQL?
- Change the Auto Increment counter in MySQL?
- What if I forgot to set Auto Increment? Can I set it later 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?
- Set custom Auto Increment with ZEROFILL in MySQL
- Passing NULL to MySQL for auto increment?
- How to change auto increment number in the beginning in MySQL?
- How to handle fragmentation of auto increment ID column in MySQL?
- How to create a table with auto-increment column in MySQL using JDBC?
