【问题标题】:Auto increment by 2 for a specific table (No php handeling)为特定表自动递增 2(无 php 处理)
【发布时间】:2014-01-28 17:08:00
【问题描述】:

我创建了一个表,我需要将每个条目的自动增量切换为 +2。我找到了一种在全球范围内进行的方法:

 setting auto increment=2
auto increment offset=2

我可以只为一张桌子这样做吗?

【问题讨论】:

    标签: mysql auto-increment


    【解决方案1】:

    auto_increment_offset 只会设置所有表格的起点,而不是增量。

    表定义或alter语句中的AUTO_INCREMENT将为该单个表设置起始auto_increment编号。

    您正在寻找的更像是server setting auto_increment_increment。不幸的是,这在 MySQL 中的单个表上不受支持。引用手册:

    It is not possible to confine the effects of these two variables to a single table, and thus they do not take the place of the sequences offered by some other database management systems; these variables control the behavior of all AUTO_INCREMENT columns in all tables on the MySQL server.

    您可以尝试滚动自己的序列,每次插入此表时插入一个单独的 auto_increment“序列”表,只需从序列表中取出 last_insert_id() 并将其乘以 2 用于其他表使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多