Units of Work
A transaction is a logical unit of work that consists of one or more SQL statements. A transaction should be all or nothing โ either all its operations complete successfully, or none of them take effect.
Example: Bank Transfer
-- Transfer $500 from Alice to Bob
BEGIN TRANSACTION;
UPDATE Accounts SET Balance = Balance - 500 WHERE Name = 'Alice';
UPDATE Accounts SET Balance = Balance + 500 WHERE Name = 'Bob';
COMMIT;
If either UPDATE fails, the entire transaction is rolled back:
Alice keeps her $500, Bob doesn't get it. No money disappears.
Transaction Control
BEGIN TRANSACTION; -- Start a transaction
-- ... SQL statements ...
COMMIT; -- Save all changes permanently
-- OR
ROLLBACK; -- Undo all changes since BEGIN