Sequence NODE_300
Medium

Wrap Multiple Statements in a Transaction

SQL
Technical Specification

Demonstrate how to transfer money between two accounts: deduct from one, add to another, safely inside a transaction.

Input/Output Samples
Input:transfer 500 from A to B
Output:Either both balances updated or none
Optimal Logic Path
BEGIN;

UPDATE accounts
SET balance = balance - 500
WHERE id = 1;

UPDATE accounts
SET balance = balance + 500
WHERE id = 2;

COMMIT;

/* On error, use ROLLBACK instead of COMMIT. */
Architectural Deep-Dive
Transactions ensure atomicity so related changes (like money transfer) don't leave data in inconsistent state.