I am planning to use PHP to execute an SQL case statement to edit many rows at once in a MySQL table. If the query fails, is it possible that some values will have changed while others haven’t?
This example shows the kind of case statement involved:
UPDATE person
SET name = CASE id
WHEN 1 THEN 'Jim'
WHEN 2 THEN 'Mike'
WHEN 3 THEN 'Precious'
END,
sex = CASE id
WHEN 1 THEN 'female'
WHEN 2 THEN 'male'
WHEN 3 THEN 'male'
END
WHERE id IN (1,2,3)
Transactions is the word you’re looking for. A transaction is an all or nothing operation. This means that either everything passes and the transaction is done, or something fails and the whole transaction is rolled back. This way you prevent inconsistencies in your data.
As mentioned above, your table engine needs to support transactions. InnoDB does this in MySQL, while MyISAM doesn’t.