O'Reilly logo

MySQL 8 Cookbook by Karthik Appigatla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required