且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

InnoDB中的auto_increment值?

更新时间:2023-01-21 20:20:07

如果您有外键约束,那么当表B引用该行时,如何从表A中删除该行?对我来说这似乎是一个错误.

If you have a foreign key constraint, how can you delete a row from table A when table B references that row? That seems like an error to me.

无论如何,通过在应用程序启动时重置偏移量,可以避免重复使用自动增量值.在所有引用表A的表中查询最大值,然后将表更改为大于该最大值的表,例如如果最大为989,请使用以下代码:

Regardless, you can avoid the reuse of auto-increment values by resetting the offset when your application starts back up. Query for the maximum in all the tables that reference table A, then alter the table above that maximum, e.g. if the max is 989, use this:

alter table TableA auto_increment=999;

还请注意,不同的MySQL引擎具有不同的自动递增行为.此技巧适用于InnoDB.

Also beware that different MySQL engines have different auto-increment behavior. This trick works for InnoDB.