如何在MYSQL中将值插入自动标识列

时间:2022-09-21 09:37:00

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

我想在mysql innodb表Auto_Increment列中插入值。

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.

我正在将旧表中的一些数据加载到具有标识的新表中,并且需要保留旧表中的现有值,因此我需要保留现有的Id值,但保留Auto_Increment列以获取新值。

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.

在MS T-SQL中,我将使用SET SET IDENTITY_INSERT MyTable ON启动插入查询脚本,并使用SET SET IDENTITY_INSERT MyTable OFF结束查询。

How can I do the same in MySQL?

我怎样才能在MySQL中做同样的事情?

1 个解决方案

#1


6  

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;

#1


6  

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;