ProblemYou want to know what happens to a sequence when you delete rows from a table that contains an AUTO_INCREMENT column. SolutionIt depends on which rows you delete and on the storage engine. DiscussionWe have thus far considered how sequence values in an AUTO_INCREMENT column are generated for circumstances where rows are only added to a table. But it's unrealistic to assume that rows will never be deleted. What happens to the sequence then? Refer again to Junior's bug-collection project, for which you currently have an insect table that looks like this: mysql> SELECT * FROM insect ORDER BY id; +----+-------------------+------------+------------+ | id | name | date | origin | +----+-------------------+------------+------------+ | 1 | housefly | 2006-09-10 | kitchen | | 2 | millipede | 2006-09-10 | driveway | | 3 | grasshopper | 2006-09-10 | front yard | | 4 | stink bug | 2006-09-10 | front yard | | 5 | cabbage butterfly | 2006-09-10 | garden | | 6 | ant | 2006-09-10 | back yard | | 7 | ant | 2006-09-10 | back yard | | 8 | millbug | 2006-09-10 | under rock | +----+-------------------+------------+------------+ That's about to change because after Junior remembers to bring home the written instructions for the project, you read through them and discover two things that affect the insect table's contents:
These instructions require that a few rows be removed from the insect tablespecifically those with id values 2 (millipede), 8 (millbug), and 7 (duplicate ant). Thus, despite Junior's evident disappointment at the reduction in the size of his collection, you instruct him to remove those rows by issuing a DELETE statement: mysql> DELETE FROM insect WHERE id IN (2,8,7); This statement illustrates one reason why it's useful to have unique ID values: they enable you to specify any row unambiguously. The ant rows are identical except for the id value. Without that column in the insect table, it would be more difficult to delete just one of them. After the unsuitable rows have been removed, the resulting table contents become: mysql> SELECT * FROM insect ORDER BY id; +----+-------------------+------------+------------+ | id | name | date | origin | +----+-------------------+------------+------------+ | 1 | housefly | 2006-09-10 | kitchen | | 3 | grasshopper | 2006-09-10 | front yard | | 4 | stink bug | 2006-09-10 | front yard | | 5 | cabbage butterfly | 2006-09-10 | garden | | 6 | ant | 2006-09-10 | back yard | +----+-------------------+------------+------------+ The sequence in the id column now has a hole (row 2 is missing) and the values 7 and 8 at the top of the sequence are no longer present. How do these deletions affect future insert operations? What sequence number will the next new row get? Removing row 2 created a gap in the middle of the sequence. This has no effect on subsequent inserts, because MySQL makes no attempt to fill in holes in a sequence. On the other hand, deleting rows 7 and 8 removes values at the top of the sequence, and the effect of this depends on the storage engine:
If a table uses an engine that differs in value-reuse behavior from the behavior you require, use ALTER TABLE to change the table to a more appropriate engine. For example, if you want to change a BDB table to be a MyISAM table (to prevent sequence values from being reused after rows are deleted), do this: ALTER TABLE tbl_name ENGINE = MyISAM; If you don't know what engine a table uses, consult INFORMATION_SCHEMA or use SHOW TABLE STATUS or SHOW CREATE TABLE to find out. For example, the following statement indicates that insect is a MyISAM table: mysql> SELECT ENGINE FROM INFORMATION_SCHEMA.TABLES -> WHERE TABLE_SCHEMA = 'cookbook' AND TABLE_NAME = 'insect'; +--------+ | ENGINE | +--------+ | MyISAM | +--------+ NOTE
In this chapter, you can assume that if a table's definition has no explicit storage engine, it's a MyISAM table. If you want to clear out a table and reset the sequence counter, use trUNCATE TABLE: TRUNCATE TABLE tbl_name; |