I have a table that looks like this
CREATE TABLE IF NOT EXISTS `language` (
`idkey` int(10) unsigned NOT NULL AUTO_INCREMENT,
`english_lang` text,
`japanese_lang` text,
PRIMARY KEY (`idkey`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1587 ;
The ‘idkey’ is just a number that starts at 1 and increments.
The ‘idkey’ currently has gaps in the numbering from entries I’ve removed and I wanted to see if it was possible to run a MySQL command to re-write this column so there are no gaps in the numbering.
eg: rewrite it so it starts at 1 and runnings till the end (around 1400 entries).
thanks
It is against standard practices to try to fill in these “missing numbers”. You need to consider that they were once assigned to something. Assigning them now to something else could potentially lead to problems–especially if you’re not using foreign key constraints and have some bad data sitting around.
If you are in a situation where you tested some data and now want to take things live, you can reset the auto-increment by running
Alter Table TABLENAME AUTO_INCREMENT=1(or any other number) or you can drop and recreate the table. If it has been in production I would recommend against resetting or trying to “fill in the gaps”.In the interest of completeness I believe you can insert a specific number in a normally auto incrementing column by including it in your insert statement. The next auto increment number should pick up from the one you specified. If it’s already taken this will cause you problems.
If you really just want to close up gaps the easiest way is to create a new table with the same structure. Then run a query like:
The drop the old table and rename the new table. The gaps will be filled in and your auto increment number will be at the “correct” place.
BUT REALLY YOU SHOULD LEAVE GAPS FROM DELETED RECORDS IN PLACE.