BEGIN, COMMIT & ROLLBACK
Execute atomic multi-step database operations with BEGIN, commit modifications, and revert failures with ROLLBACK.
BEGIN, COMMIT & ROLLBACK is an essential topic in SQL and relational database engineering.
Overview
Building on foundational principles introduced in acid-properties, mastering begin, commit & rollback provides the core mental model and practical query skills needed for production relational database engineering.
BEGIN TRANSACTION;
-- Step 1: Deduct from Alice
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
-- Step 2: Add to Bob
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
-- Step 3: Commit both atomically
COMMIT;
-- If an error occurs, execute ROLLBACK instead!
Key Concepts
- Understand the fundamental syntax, conventions, and mental model.
- Avoid common beginner traps, data type mismatches, and performance pitfalls.
- Apply practical idioms directly in production applications.
[!TIP] Master this concept to build a rock-solid foundation. Next, proceed to transaction-isolation-levels.