- 实际上,我已经编写了一段代码来在 MySQL 表中删除和添加新用户,并且两者都可以正常工作。
- id为主键,自增。
- 问题是假设最后一个条目的 ID 为“9”,当我删除该条目并添加一个新条目时,新条目的 ID 为“10”而不是“9”。
最佳答案
根据 3.6.9 Using AUTO_INCREMENT和 15.6.1.5 AUTO_INCREMENT Handling in InnoDB一旦生成了 auto_increment
值,它就不会被重复使用。从表中删除行时,您会在 auto_increment
数字序列中观察到间隙。
即使插入事务有回滚:
if a transaction that generated auto-increment values rolls back, those auto-increment values are “lost”. Once a value is generated for an auto-increment column, it cannot be rolled back, whether or not the “INSERT-like” statement is completed, and whether or not the containing transaction is rolled back. Such lost values are not reused. Thus, there may be gaps in the values stored in an AUTO_INCREMENT column of a table.
关于java - Int ID在MySQL中删除和添加条目时跳转数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53342430/