Transaction scenario
- SQL course
- Intermediate
- Lesson 115/115
- 6 min
Write a safe transfer with a transaction.
Transaction scenario
In this challenge you will write a money transfer transaction: BEGIN, two UPDATEs, COMMIT.
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;A safe money transfer.
Practice Transaction scenario
Write a transfer transaction: BEGIN, two UPDATEs, COMMIT.
Starter code
-- write scenarioSample solution
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;Expected output
Transaction tamamlandı
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Add COMMIT
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
____;
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
Explanation: After both UPDATEs, COMMIT makes the two steps of the transfer permanent together.
2 What commits a transaction?
- BEGIN
- COMMIT Correct answer
- ROLLBACK
- SAVEPOINT
Explanation: COMMIT commits a transaction.