【问题标题】:Cannot set initial value for AUTO_INCREMENT in MySQL无法在 MySQL 中为 AUTO_INCREMENT 设置初始值
【发布时间】:2018-08-02 06:47:24
【问题描述】:

首先,我创建列 img_ids,稍后我想将其递增,初始值为 393。

下一步,我执行以下操作:

Alter table img_ids_for_media modify image_id int(11) AUTO_INCREMENT PRIMARY KEY

这给了我简单的增量 1,2,3...

在此之后我尝试:

 Alter table img_ids_for_media  AUTO_INCREMENT = 393

这个查询通过了,但什么也没做。我还有1,2,3,没有

393,393,394

我能用这个做什么?

【问题讨论】:

    标签: mysql database auto-increment


    【解决方案1】:

    问题在于,更改表的 auto_increment 值会设置要使用的 下一个 auto_increment 值,它不会影响字段中的现有值 - 没错。

    您可以做的是更新现有的auto_increment 值,向它们添加 392 (image_id=image_id+392)。

    然后确保将下一个auto_increment 值设置为max(image_id)+1

    【讨论】:

      【解决方案2】:

      如何在 MySQL 中设置初始值和自增已经回答了?

      查看此链接

      How to set initial value and auto increment in MySQL?

      【讨论】:

      • 这正是 OP 所做的并最终提出了这个问题。
      猜你喜欢
      • 2021-05-07
      • 1970-01-01
      • 2011-12-22
      • 2015-03-25
      • 2018-08-07
      • 1970-01-01
      • 2014-10-08
      • 1970-01-01
      • 2022-07-21
      相关资源
      最近更新 更多