
- 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 handle fragmentation of auto increment ID column in MySQL?
Whenever we renumber, there might be a problem. There is a need to declare a unique ID for a column.
In MySQL version 5.6 InnoDB, we can reuse the auto_increment ID by including the ID column in an INSERT statement and we can give any specific value that we want.
The situations are as follows −
- Whenever we delete the ID with the highest number
- Whenever we start and stop MySQL server
- Whenever we insert a new record
Example of ID auto increment using auto_increment variable.
mysql> create table UniqueAutoId -> ( -> id int auto_increment, -> Unique(id) -> ); Query OK, 0 rows affected (0.45 sec)
Inserting records into table.
mysql> insert into UniqueAutoId values(); Query OK, 1 row affected (0.13 sec) mysql> insert into UniqueAutoId values(); Query OK, 1 row affected (0.16 sec) mysql> insert into UniqueAutoId values(); Query OK, 1 row affected (0.07 sec) mysql> insert into UniqueAutoId values(); Query OK, 1 row affected (0.10 sec) mysql> insert into UniqueAutoId values(); Query OK, 1 row affected (0.10 sec)
Displaying all records.
mysql> select *from UniqueAutoId;
The following is the output.
+----+ | id | +----+ | 1 | | 2 | | 3 | | 4 | | 5 | +----+ 5 rows in set (0.00 sec)
To delete records, we have used DELETE statement. Here, we are deleting id=5;
mysql> DELETE from UniqueAutoId where id=5; Query OK, 1 row affected (0.14 sec)
Displaying all records.
mysql> select *from UniqueAutoId;
The following is the output.
+----+ | id | +----+ | 1 | | 2 | | 3 | | 4 | +----+ 4 rows in set (0.00 sec)
Let us again delete a record from the table.
mysql> delete from UniqueAutoId where id=2; Query OK, 1 row affected (0.15 sec)
Again, displaying records from the table.
mysql> select *from UniqueAutoId;
The following is the output.
+----+ | id | +----+ | 1 | | 3 | | 4 | +----+ 3 rows in set (0.00 sec
The above results in fragmentation.
- Related Articles
- MySQL query to set my auto increment column ( id ) to zero or reset the value of auto increment field?
- How to get the next auto-increment id in MySQL?
- How to add auto-increment to column in MySQL database using PhpMyAdmin?
- Display auto increment user id sequence number to begin from 001 in MySQL?
- How to create a table with auto-increment column in MySQL using JDBC?
- Auto increment in MongoDB to store sequence of Unique User ID?
- How to change auto increment number in MySQL?
- Set MySQL int column to auto increment by 1 beginning at 10000?
- How to auto-increment value of tables to lower value in MySQL?
- How to set initial value and auto increment in MySQL?
- How to make MySQL table primary key auto increment?
- How to increment all the rows of a particular column by 1 in a single MySQL query (ID column +1)?
- Passing NULL to MySQL for auto increment?
- How to change auto increment number in the beginning in MySQL?
- Change the Auto Increment counter in MySQL?

Advertisements