【问题标题】:MySQL temporarily disable auto increment ID for data migrationMySQL 暂时禁用自动增量 ID 进行数据迁移
【发布时间】: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 可以插入我自己的值而不禁用它。

标签: mysql database


【解决方案1】:

仅基于 P.Salmon 的评论,当您同时向 id 和其他字段插入值时,似乎应该没有任何问题。

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

ALTER TABLE new_table AUTO_INCREMENT = 100;

【讨论】:

    猜你喜欢
    • 2017-06-11
    • 2012-11-16
    • 2016-02-09
    • 2020-01-29
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    • 2020-05-25
    相关资源
    最近更新 更多