业务场景:
表 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;
本文介绍如何在保持历史数据的情况下,将table_a表中的非主键id字段转换为唯一主键,并实现自增。通过SQL语句逐步操作,确保新旧数据的连续性和一致性。
4216

被折叠的 条评论
为什么被折叠?



