我一直在为一个项目使用 InnoDB,并且依赖于 auto_increment。这对大多数表来说不是问题,但对于删除的表来说,这可能是个问题:
AUTO_INCREMENT Handling in InnoDB
特别是这部分:
AUTO_INCREMENT column named ai_col: After a server startup, for the first insert into a table t, InnoDB executes the equivalent of this statement: SELECT MAX(ai_col) FROM t FOR UPDATE; InnoDB increments by one the value retrieved by the statement and assigns it to the column and to the auto-increment counter for the table.
这是一个问题,因为虽然它确保表内的键是唯一的,但该表的外键不再是唯一的。
mysql 服务器确实/不应该经常重新启动,但这正在中断。有什么简单的方法可以解决这个问题吗?
最佳答案
如果有外键约束,当表 B 引用该行时,如何从表 A 中删除该行?这对我来说似乎是个错误。
无论如何,您可以通过在应用程序重新启动时重置偏移量来避免重复使用自动增量值。查询引用表 A 的所有表中的最大值,然后更改该最大值以上的表,例如如果最大值是 989,使用这个:
alter table TableA auto_increment=999;
另请注意,不同的 MySQL 引擎具有不同的自动增量行为。这个技巧适用于 InnoDB。
关于mysql - InnoDB 中的 Auto_increment 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1972153/