【发布时间】:2011-10-13 19:38:34
【问题描述】:
我看到很多几乎相似的问题都有明显的答案,但我很确定这里还没有这个问题。
我需要在现有表中添加一个自动递增的 id 列并将其设置为主键。我不能丢失任何现有数据。
我可以成功地更改表结构,但我收到有关新列中截断数据的错误。当我查看数据时,新的自动递增列中的每个值都是空的(因此不是唯一的)。
如何回填这些值以确保主键的唯一性?
***如果有更简单的解决方案,我希望避免将现有数据转储到临时表并重新插入。
当前脚本:
alter table the_table add new_field int first;
alter table the_table drop primary key, add primary key (new_field);
alter table the_table change new_field new_field int unsigned not null auto_increment;
我按此顺序运行脚本,因为我不能有一个不是主键的自动递增列。
(MySQL 5.3)
【问题讨论】:
标签: mysql sql auto-increment