CodeBlanks Logo Icon CodeBlanks

Transaction scenario

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

query.sql
-- write scenario

Sample solution

query.sql
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

Question
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
____;
Answer
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.

All lessons in Intermediate