17

When using MySQL START TRANSACTION and the decision is made by MySQL to roll back - In the case that a table had an AUTO_INCREMENT column - does the column get... decremented during the roll back?

Or should it?

I am having some issues where the transaction data is being properly rolled back - but it looks like the table was auto incremented and not decremented in the rollback.


# BOTH TABLES START OUT EMPTY // TABLE1 ID is **auto_increment** 

START TRANSACTION;

INSERT INTO `TABLE1` (`ID` ,`NAME`) VALUES (NULL , 'Ted');  # MySQL TABLE1 **ID** is Auto incremented to 1 

INSERT INTO `TABLE2` (`ID` ,`WRONGVALUE`) VALUES (NULL , 'some value');  # error. This TRANSACTION will not succeed 

COMMIT;  # Because of the error - this TRANSACTION is now rolled back and Ted is NOT added


Because MySQL will auto_increment the ID on the first table - regardless of if the transaction succeeds or fails - is the standard practice for this to decrement the table yourself?

2
  • I believe that autoincrement once it goes up, it wont go back
    – jcho360
    Commented Feb 7, 2013 at 18:48
  • as far as I know, there's no way a transaction is partially completed... is either fully completed or fully rolled-back (that's the point of a transaction)... besides that, I agree with jcho360
    – Barranka
    Commented Feb 7, 2013 at 18:52

2 Answers 2

55

No, auto-increment mechanisms must work outside the scope of transactions, because another user may be inserting to the same table before you finish your transaction. The other user's transaction must be able to allocate the next value, before knowing whether your transaction is using the value you just allocated.

Re your comment: If I may say it more clearly, any change in the scope of a transaction may be rolled back. The auto-increment counter is not rolled back, so it doesn't obey atomicity of transactions. Nor does it obey isolation because another transaction gets the next value even though your transaction hasn't committed yet.

The way auto-increment works means that sometimes, if you insert some rows and then roll back your transaction, the values you allocated with auto-increment are lost forever!

But this is okay. Primary key values must be unique, but they don't need to be consecutive. In other words, they are not row numbers, and you shouldn't use them like that. So you should never need to decrement values created by auto-increment.

5
  • +1 Wonderful answer... I knew that auto-increments worked outside of the scope of transactions, but I never made the connection to that being needed for concurrency. Commented Feb 7, 2013 at 18:52
  • @Bill Karwin Hey Bill? I just wanted to ask if you thought my wording was correct in the question "# Because of the error - this TRANSACTION is now rolled back and Ted is NOT added" - Is that accurate to say - or is it actually "Rolled Back" - Or did it even go forward.... I guess what I'm trying t say is - is the term ROLLED BACK saying that part of the data was temporarily changed in the table... and then "ROLLED BACK" as in the temporary data was removed?
    – user1502852
    Commented Feb 8, 2013 at 21:34
  • @KDawg, yes, InnoDB logs and stores all changes, and if it's an update or delete, copies of the old rows are also preserved. If you roll back, then the new versions are discarded and original versions are restored. This happens asynchronously in the background as a kind of garbage-collection (that is, it may happen minutes or even hours later). Even before that happens, InnoDB can transparently return the correct version of any row you query. Commented Feb 8, 2013 at 21:39
  • The downside that i see is that, in the dev/test environment, you can reach a tinyint id very fast. Commented Sep 8, 2014 at 19:17
  • 2
    @RafaelBarros, that is true, tinyint is often too small a range for most tables. But that has nothing to do with isolation of auto-increment. Commented Sep 8, 2014 at 20:44
1

Disclosure: I am relatively new to SQL so some of this may be wrong. But this is how I understand it. Auto increment must function outside of transactions otherwise you can compromise data. Let us say you have 2 users both trying to connect to your dB. Both are creating accounts. When Account 1 is being created it increments up to 1. Then Account 2 is created and it increments to 2. So far it would look like the following:

  • (1, Account1)
  • (2, Account2)
  • Increment: 2.

Now let us say that Account 2 commits, but Account 1 fails. Now your table looks as follows:

  • (2, Account2)
  • Increment: 1.

Because you decremented your Auto Increment, you will fail at having a unique value when someone tries to register a new account, because the pair will be:

  • (2,Account2)
  • Attempting to Insert (2, NewAccount)

Which will fail because 2, the primary key, will not be unique.

If you need to fix your table (for development purposes) you can always run the following code:

ALTER TABLE `DB_NAME`.`TABLE_NAME` 
AUTO_INCREMENT = 1 (or some other number);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.