【发布时间】:2021-10-26 21:25:02
【问题描述】:
我需要将旧数据插入新数据库,我想将旧数据库 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)?
【问题讨论】:
-
不清楚要求是什么,但也许'当您将任何其他值插入 AUTO_INCREMENT 列时,该列设置为该值并重置序列,以便下一个自动生成的值从最大列值' - 见dev.mysql.com/doc/refman/8.0/en/example-auto-increment.html
-
谢谢!我不知道自动增量 id 可以插入我自己的值而不禁用它。