Solve the problem of inconsistency in MySQL's self-increasing primary key ID after deleting data

First we need to cancel the auto-increment and primary key of id. The following
code takes the id column in the water table as an example.

alter table water
    modify id int not null;

alter table water
    drop primary key;

Then regenerate the id column

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

The next step is to reset it to primary key + auto-increment

alter table water
    add primary key (id);

alter table water
    modify id int auto_increment;

successfully solved
Insert image description here
Insert image description here

Guess you like

Origin blog.csdn.net/weixin_51461002/article/details/131426300