MySQL autoincrement value increases even when insertion fails due to error

隐身守侯 提交于 2020-02-03 21:49:29

问题


As I receive data from the user, I validate the format of the input every time (PHP). The only thing I can't validate (or I wouldn't want to) is the constraint (for example foreign key) violation. I let the DML to raise and error and the database driver to throw an exception. After catching it I just print out some error message by MySQL's error code.

The problem is, even when insertion fails, I see that the autoincrement sequence still grows, increases. How can I prevent that?


回答1:


That is how auto increment values and sequences work. When a value is used, it is not reclaimed if the transaction fails, and the transaction is rolled back.

Since you are using auto increment values, the actual value does not matter so the spaces should not be a problem, apart from the aesthetics of having missing numbers.



来源:https://stackoverflow.com/questions/10108593/mysql-autoincrement-value-increases-even-when-insertion-fails-due-to-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!