Source: 'MySQL, The definite guide to using, programming and administering MySQL 4.1 and 5.0, 3rd edition' by Paul Dubois
Quote:
The sequence might be affected if you delete rows from the table. That is, sequence values might be reused; it is storage engine-dependent whether this occurs
|
For
MyISAM tables, the normal behaviour is not to reuse. However, you can work around it by making the auto-increment column part of a composite index.
Code:
create table mytable
{
columnA char(1) default 'a' NOT NULL,
id INT auto_increment NOT NULL,
name char(20),
..
other colmns here
..
PRIMARY KEY (columnA, id)
} ENGINE=MYISAM;
The trick is to always insert the same value in columnA. If you now delete a record, MYISAM will reuse deleted id (according to the book, not tested). Disadvantage is obviously the sue of extra space due to the extra column.
BDB tables do reuse deleted values
InnoDB tables and
MEMORY tables do not reuse deleted values
Notes:
1)
The id column MUST be specified as the last field in the primary key (so
id, columnA wil not work)
2)
The given example allows the occurence of the same id a number of times
Code:
columnA id name
a 1 shan_nathan
a 2 rawdata
b 1 wim sturkenboom
a 3 someone else
3)
As far as I know, deleted values will be reused when the end of the range is reached