0

I want to know whether the following sql statement will be auto rollback to state before start transaction should there is error occur in any of the insert/update. Or do I manually need to do rollback to initial state before start transaction? If I need to manually do rollback, how should I do it?

START TRANSACTION;
INSERT INTO TABLE 1 ...
UPDATE TABLE 1 ...
UPDATE TABLE 2 ...
INSERT TABLE 3 ...
COMMIT;
Barmar
  • 741,623
  • 53
  • 500
  • 612
eulercode
  • 1,107
  • 4
  • 16
  • 29

1 Answers1

0

You can rollback a MySQL transaction using ROLLBACK;. Automatic rollback depends on the client which you are using. If you are using, the MySql command line client, it triggers a quit when an error occurs, which would rollback any pending transactions. However, some clients may not automatically trigger a rollback when an error is returned, leaving you to handle the error as you see fit. You can see more information about how transactions work, including their various options and nuances on the official documentation.

Edit: Updated answer about automatic rollback using information from an answer in this other question.

kfan
  • 46
  • 1
  • 11