How to do it...

To start a transaction (set of SQLs), execute the START TRANSACTION or BEGIN statement:

mysql> START TRANSACTION;or mysql> BEGIN;

Then execute all the statements that you wish to be inside a transaction, such as transferring 100 from A to B:

mysql> SELECT balance INTO @a.bal FROM account WHERE account_number='A';Programmatically check if @a.bal is greater than or equal to 100 mysql> UPDATE account SET balance=@a.bal-100 WHERE account_number='A';mysql> SELECT balance INTO @b.bal FROM account WHERE account_number='B';Programmatically check if @b.bal IS NOT NULL mysql> UPDATE account SET balance=@b.bal+100 WHERE account_number='B';

After making sure that all the SQLs are executed successfully, execute the COMMIT statement, which ...

Get MySQL 8 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.