mysql - 特定表自动递增 2(无 php 处理)

标签 mysql auto-increment

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

 setting auto increment=2
auto increment offset=2

我可以只对一张 table 执行此操作吗?

最佳答案

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

表定义或 alter 语句中的 AUTO_INCRMENT 将为该单个表设置起始 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即可用于其他表使用。

关于mysql - 特定表自动递增 2(无 php 处理),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21412139/

相关文章:

php - 如何通过 MySQL 查询使用 BirthDate 列获取以月为单位的年龄?

hibernate - 使用 Hibernate 注释映射 PostgreSQL 串行类型

reactjs - 如何在React Native中每次调用onPress函数时提供自动增量?

ruby-on-rails - 我可以创建一个 ID 从 0 而不是 1 自动递增的 Rails 模型吗?

mysql - 如何在 BigQuery SQL 中将一组用户分组为 10 人组?

mysql - 如何在mysql的一组数据中使用like函数?

mysql - 如何在mysql中的日期中添加一年零两天

MySQL 5.6.10安装报错

mysql - hibernate 不在 mysql 表上生成自动增量约束

java - 指定为 1 时 Derby 自动递增 100