Skip to main content
Practice

10

sql

-- Transaction simulation

-- Begin transaction
BEGIN;

-- Subtract 100 coins from Alice
UPDATE accounts
SET balance = balance - 100
WHERE name = 'Alice';

-- Add 100 coins to Bob
UPDATE accounts
SET balance = balance + 100
WHERE name = 'Bob';

-- Commit the transaction
COMMIT;

-- If one of the operations fails, the entire transaction will be rolled back
-- and the database will remain in the same state as before the transaction
-- This is the power of ACID transactions

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.