将自动增量主键插入现有表

将自动增量主键插入现有表

问题描述:

我试图更改一个既没有主键又没有auto_increment列的表.我知道如何添加主键列,但我想知道是否有可能自动将数据插入主键列(我已经在数据库中有500行,并希望为其提供ID,但我不想手动执行) .有什么想法吗?非常感谢.

I am trying to alter a table which has no primary key nor auto_increment column. I know how to add an primary key column but I was wondering if it's possible to insert data into the primary key column automatically (I already have 500 rows in DB and want to give them id but I don't want to do it manually). Any thoughts? Thanks a lot.

添加PRIMARY KEY列的ALTER TABLE语句在我的测试中可以正常工作:

An ALTER TABLE statement adding the PRIMARY KEY column works correctly in my testing:

ALTER TABLE tbl ADD id INT PRIMARY KEY AUTO_INCREMENT;

在为测试目的而创建的临时表上,以上语句创建了AUTO_INCREMENT id列,并为表中的每个现有行插入了从1开始的自动增量值.

On a temporary table created for testing purposes, the above statement created the AUTO_INCREMENT id column and inserted auto-increment values for each existing row in the table, starting with 1.