In MySQL I used INSERT IGNORE statement to insert rows to table. Because one column is UNIQUE, some rows were not inserted (as they already been there). After execution of that statement I noticed that auto increment column has some missing numbers between rows, which later I realized that happened due to rows that was ignored and not added.
Is it possible to setup system to not increase auto increment counter if no row is inserted with IGNORE clause?
Quoting from the manual page for INSERT:
The
INSERT IGNOREsyntax is just a way to suppress certain error messages and it’s helpful when you are aware that those errors might happen and/or want to handle them at a later stage. Behind the scenes, you still have a regular insert, except that it fails due to a violated key. MySQL needs the actual row values to make an insert and the AUTO_INCREMENT counter will increment according to regular rules:So unless you can rethink your logic (e.g., test whether the key values exist before making the insert), the only way to reset the counter is ALTER TABLE:
It is not that gaps should matter anyway. If they do (e.g., you’re generating invoice numbers that need to be correlative), you’re probably using the wrong tool for the job.