How to insert values into auto identity column in MYSQL [closed]

杀马特。学长 韩版系。学妹 提交于 2019-12-23 07:20:11

问题


I would like to insert values into mysql innodb table Auto_Increment column.

I am loading some data from an old table to new table which has an identity, and need to preserve the existing values from the old table, so I need to preserve the existing Id values, but keep the column Auto_Increment for new values.

In MS T-SQL, I would start my insert query script with SET SET IDENTITY_INSERT MyTable ON and end the query with SET SET IDENTITY_INSERT MyTable OFF.

How can I do the same in MySQL?


回答1:


Just do as usual:

INSERT INTO my_table(auto_inc_field, other_field) VALUES(8547, 'some value');

If the values are comming from another table, you may use:

INSERT INTO my_table(auto_inc_field, other_field)
SELECT auto_inc_field, other_field FROM other_table;


来源:https://stackoverflow.com/questions/16108003/how-to-insert-values-into-auto-identity-column-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!