解决MySQL删除数据后自增主键ID不连贯问题

首先我们需要取消id的自增和主键
下列代码以water表中的id列为例

alter table water
    modify id int not null;

alter table water
    drop primary key;

然后重新生成id列

set @i=0;
update water set water.id=(@i:=@i+1);

下一步就是重新设置为主键+自增

alter table water
    add primary key (id);

alter table water
    modify id int auto_increment;

成功解决
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_51461002/article/details/131426300
今日推荐