I've seen here that MySQL does not re-index PRIMARY KEY's with AUTO_INCREMENT when records are deleted from the table. That is, if I have
1 John Connor 12/05/1973
2 Peter Brown 04/03/1979
.....
50 Mike Kennedy 01/12/1975
and I delete records starting at ID = 3
and ending at ID = 50
, and then insert the following (in this exact same sequence, one after another):
Mark Carney 09/02/1970
Jimmy Johnson 14/09/1968
Roman Harley 19/05/1972
I would end up with:
1 John Connor 12/05/1973
2 Peter Brown 04/03/1979
53 Roman Harley 19/05/1972
52 Jimmy Johnson 14/09/1968
51 Mark Carney 09/02/1970
When more records are inserted, the column ID increments just as expected (i.e. 54, 55, 56, ...
), however the order of records becomes reversed. In the example above, 53
is inserted above 52
, and 52
is above 51
. Isn't the result supposed to be
1 John Connor 12/05/1973
2 Peter Brown 04/03/1979
51 Mark Carney 09/02/1970
52 Jimmy Johnson 14/09/1968
53 Roman Harley 19/05/1972
as our common sense would tell us? Why are the newer records placed on top of the older ones? If this has to do with the "table cells" (which were vacated once the records from ID = 3
to ID = 50
were deleted), then why doesn't 51
take place of 3
, 52
place of 4
, and 53
place of 5
? Is there a way to change this behavior?
P.S. I am storing data with MySQL, querying with PHP, and managing the DB with phpMyAdmin. The PK in the table is set as follows: ID INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY
.
Thank you in advance!