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.
BEGIN TRANSACTION in SQL?BEGIN TRANSACTION doesBEGIN TRANSACTION;.START TRANSACTION; is valid in some systems but not standard SQL; BEGIN; alone is incomplete; TRANSACTION START; is invalid syntax.BEGIN TRANSACTION; UPDATE accounts SET balance = balance - 100 WHERE id = 1; UPDATE accounts SET balance = balance + 100 WHERE id = 2; COMMIT;
BEGIN TRANSACTION; and ended by COMMIT;.BEGIN TRANSACTION
INSERT INTO users (name) VALUES ('Alice');
COMMIT;BEGIN TRANSACTION is missing a semicolon at the end, which is required to separate statements.COMMIT; correctly comes after the insert; the insert syntax is valid.BEGIN TRANSACTION; UPDATE accounts SET balance = balance - 200 WHERE id = 1; UPDATE accounts SET balance = balance + 200 WHERE id = 2; COMMIT;uses
BEGIN TRANSACTION and COMMIT to group both updates safely. UPDATE accounts SET balance = balance - 200 WHERE id = 1; UPDATE accounts SET balance = balance + 200 WHERE id = 2;lacks transaction, risking partial update.
BEGIN TRANSACTION; UPDATE accounts SET balance = balance + 200 WHERE id = 2; COMMIT;only credits without debit.
BEGIN; UPDATE accounts SET balance = balance - 200 WHERE id = 1; ROLLBACK;rolls back, so no change occurs.
BEGIN TRANSACTION; UPDATE accounts SET balance = balance - 200 WHERE id = 1; UPDATE accounts SET balance = balance + 200 WHERE id = 2; COMMIT;-> Option A
Pass the test and this pattern moves unknown