Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is 'update tokens set tokens = tokens + 1' atomic in InnoDB?

Without using explicit transactions, is:

update tokens set tokens = tokens + 1

guaranteed to be atomic in InnoDB?

like image 817
Limbeh Avatar asked Dec 06 '11 21:12

Limbeh


1 Answers

My understanding is that in autocommit mode (i.e. "without explicit transactions") a single statement is a single transaction. As a transaction is per definition atomic, so your single statement is atomic as well.

However, when it comes to evaluating constraints, this is not done on the statement (i.e. transaction) level, but row by row while the statement is processed.

If you have a unique constraint (index) on the tokens column, that update will most probably fail because of that. The same is true for foreign keys referencing the same table.

like image 99
a_horse_with_no_name Avatar answered Sep 21 '22 03:09

a_horse_with_no_name