我有一个 mysql 表,其中有超过 17000 行。我从其中的一些部分删除了大约 530 行。现在,每一行都有一个连续的自动递增数字主键。正如您现在可以理解的那样,行的多个数字已被删除。所以我只是想问有没有办法以某种完美的顺序再次修复所有行?
最佳答案
您可以但要小心其他表将此主键用作外键
SET @count = 0;
UPDATE table SET table.id = @count:= @count + 1;
这将更新表table
的id
列...然后您需要重置auto_increment:
ALTER TABLE table AUTO_INCREMENT = 1;
这会将 docs 中的下一个 id 重置为 MAX(id)+1
:
To change the value of the AUTO_INCREMENT counter to be used for new rows, do this:
ALTER TABLE t2 AUTO_INCREMENT = value;
You cannot reset the counter to a value less than or equal to any that have already been used. For MyISAM, if the value is less than or equal to the maximum value currently in the AUTO_INCREMENT column, the value is reset to the current maximum plus one
关于mysql - 删除其中一些后修复 mysql 表行 id 中的间隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46529882/