MySQL暂时禁用数据迁移的自动增量ID



我需要插入旧数据到一个新的数据库,我想保持旧的数据库ID值为新的数据库ID(新的数据库ID字段是自动增量ID)。那么我如何为MySQL中的自动增量ID做到这一点(无法导出/导入,因为我没有正确的要做到这一点,我只能从旧数据库插入数据(新数据库和旧数据库的结构不同),那么我怎么做呢?

## how to disable the auto increment id in new_table?
insert into new_table(id, new_field1) select id, old_field1 from old_table;
## how to re-enable the auto increment id and set the initial value to the max(id)?

根据p . salmon的评论,当您同时向id和其他字段插入值时,似乎应该不会出现任何问题。

但是,如果出于某种原因需要更改表中下一个插入行的id,则可以使用alter table语句。就像p . salmon链接到https://dev.mysql.com/doc/refman/8.0/en/example-auto-increment.html

页面上的例子一样ALTER TABLE new_table AUTO_INCREMENT = 100;

最新更新