Mysql self-increasing id problem, how can the uninterrupted id be restored to continuous?

for example, there is a table now, and id increases itself.
has a total of 100 pieces of data. When deleting 2 pieces of data from 50 items and deleting 2 pieces of data where 80 items are deleted
how to make id continuous?

Mar.05,2022

depends on your needs, because the general self-increment id is related to joining other tables, so forced recovery of continuous self-increment is a disaster

if you ignore demand, there are two ways:

1 delete the self-incrementing field, and then rebuild

ALTER TABLE `tablename` DROP COLUMN `id`;
ALTER TABLE `tablename` ADD `id` int(10) unsigned NOT NULL AUTO_INCREMENT FIRST,ADD PRIMARY KEY (`id`), AUTO_INCREMENT = 0 ROW_FORMAT = COMPACT;

2 resets the self-incrementing field values sequentially through the sql statement

SET @i=0;
UPDATE `tablename` SET `id`=(@i:=@i+1);
ALTER TABLE `tablename` AUTO_INCREMENT=0;
Menu