mysql - 删除其中一些后修复 mysql 表行 id 中的空白

标签 mysql

<分区>

我有一个 mysql 表,其中有超过 17000 行。我从它的中间部分删除了大约 530 行。现在每一行都有一个连续的 AUTO-INCREAMENTED 数字主键。正如您现在所理解的那样,行的几个数字已被删除。所以我只是想问一下,有没有办法以某种完美的顺序再次修复所有行?

最佳答案

您可以但要小心其他表使用这个主键作为外键

SET @count = 0;
UPDATE table SET table.id = @count:= @count + 1;

这将更新表 tableid 列 ... 然后您需要重置 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/9416471/

相关文章:

php - 使用 ComboBox 刷新页面而不刷新

mysql - 当我尝试在 MySQL 中创建表时,它给出错误代码 1215 : “Cannot add foreign key constraint”

php - 如何在 PHP 脚本中为特定 IP 地址生成唯一的许可证 key ?

mysql - 如何优化这个数据表?

mysql - Siteground 托管到 AWS RDS 连接花费太多时间

php - 从数据库中获取表上方的名称列

MySql 过程无法正确过滤字符串输入的查询结果

php - 从 mysql 数据库回显随机 ID 号而不重复数字?

mysql - 在生成电子邮件时解密 MySQL 密码以通知用户密码

php - VBA (Access 2013) HTTP POST 不传递参数