Concept Flow - BEGIN TRANSACTION syntax
Start
BEGIN TRANSACTION
Execute SQL Statements
COMMIT or ROLLBACK
End Transaction
This flow shows starting a transaction, running SQL commands, then either saving changes with COMMIT or undoing with ROLLBACK.
BEGIN TRANSACTION; UPDATE accounts SET balance = balance - 100 WHERE id = 1; UPDATE accounts SET balance = balance + 100 WHERE id = 2; COMMIT;
| Step | Action | Transaction State | Effect on Data | Notes |
|---|---|---|---|---|
| 1 | BEGIN TRANSACTION | Transaction started | No data changed yet | Transaction begins, changes will be grouped |
| 2 | UPDATE accounts SET balance = balance - 100 WHERE id = 1 | Transaction active | Account 1 balance decreased by 100 (pending) | Change is not yet permanent |
| 3 | UPDATE accounts SET balance = balance + 100 WHERE id = 2 | Transaction active | Account 2 balance increased by 100 (pending) | Change is not yet permanent |
| 4 | COMMIT | Transaction ended | All changes saved permanently | Changes become visible to others |
| 5 | End | No active transaction | Data is consistent and saved | Transaction complete |
| Variable | Start | After Step 2 | After Step 3 | After Step 4 |
|---|---|---|---|---|
| Transaction State | No transaction | Active | Active | No transaction |
| Account 1 Balance | Original | Original - 100 (pending) | Original - 100 (pending) | Original - 100 (committed) |
| Account 2 Balance | Original | Original | Original + 100 (pending) | Original + 100 (committed) |
BEGIN TRANSACTION starts a group of SQL commands as one unit. Changes inside are temporary until COMMIT saves them. ROLLBACK cancels all changes in the transaction. Use transactions to keep data consistent and safe.