Transactions & ACID Properties

📄 MySQLLesson 10Intermediate

Transactions group SQL operations into atomic statements that execute successfully as a group, or revert database modifications on errors.

1 SQL Transaction Syntax
SQL — Transaction Blocks
START TRANSACTION;

# Deduct user balance
UPDATE accounts SET balance = balance - 100 WHERE id = 1;

# Add payee balance
UPDATE accounts SET balance = balance + 100 WHERE id = 2;

# Commit alterations if everything succeeded
COMMIT;

# Revert modifications in case of error conditions
# ROLLBACK;
2 Code Challenge
Challenge: Write a transaction script representing order completion: inserting a record into an orders table, updating product stock, and committing changes atomically.