业务场景:
表 table_a 中有字段 id ,非主键,且有重复值。现在要将字段 id 设置为唯一主键(自增),同时将历史数据的 id 改为从 1 开始的自增.
执行以下sql 即可
select @t:=0;
update table_a set id=(@t:=@t+1);
alter table table_a add primary key(id);
alter table table_a change id id int(11) not null auto_increment;