SQL statements are atomic. That is, if you execute something like this:
UPDATE Cars SET Sold = Sold + 1
Nobody can change the Sold
variable during this statement. It is always incremented by 1, even if somebody else is executing the same statement concurrently.
The problem occurs if you have statements that depend on each other:
a = SELECT Sold FROM Cars;
UPDATE Cars SET Sold = a + 1;
Between these queries, another user can change the table Cars and update Sold. To prevent this, wrap it in a transaction:
BEGIN;
a = SELECT Sold FROM Cars;
UPDATE Cars SET Sold = a + 1;
COMMIT;
Transactions are supported by InnoDB, but not by MyISAM.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…