MySQL AUTO_INCREMENT 不回滚
我使用 MySQL 的 AUTO_INCREMENT 字段和 InnoDB 来支持事务.我注意到当我回滚事务时,AUTO_INCREMENT 字段没有回滚?我发现它是这样设计的,但是有什么解决方法吗?
I'm using MySQL's AUTO_INCREMENT field and InnoDB to support transactions. I noticed when I rollback the transaction, the AUTO_INCREMENT field is not rollbacked? I found out that it was designed this way but are there any workarounds to this?
推荐答案
让我指出一些非常重要的事情:
Let me point out something very important:
也就是说,除了比较它们是否相等(=)或不相等(<>)之外,您不应该做任何其他事情.没有关系运算符(<、>),没有按索引排序等.如果你需要按添加日期"排序,有一个添加日期"列.
That is, other than comparing them for equality (=) or unequality (<>), you should not do anything else. No relational operators (<, >), no sorting by indexes, etc. If you need to sort by "date added", have a "date added" column.
把它们当作苹果和橙子:问苹果和橙子是否一样有意义吗?是的.问苹果是否比橙子大有意义吗?不.(实际上,确实如此,但你明白我的意思.)
Treat them as apples and oranges: Does it make sense to ask if an apple is the same as an orange? Yes. Does it make sense to ask if an apple is larger than an orange? No. (Actually, it does, but you get my point.)
如果你坚持这个规则,自动生成索引的连续性差距不会导致问题.
If you stick to this rule, gaps in the continuity of autogenerated indexes will not cause problems.
相关文章