CodeBlanks Logo Icon CodeBlanks

BEGIN and COMMIT

Open and close a transaction.

BEGIN and COMMIT

BEGIN starts the transaction, COMMIT makes changes permanent.

BEGIN;
INSERT INTO orders (id, amount) VALUES (1, 50);
COMMIT;

The order is permanently added.

BEGIN and COMMIT — details

Until COMMIT, changes are temporary. After COMMIT, other sessions can see them.

For example, the two updates between BEGIN; UPDATE …; UPDATE …; COMMIT; become visible to other users at the same moment.

Practice BEGIN and COMMIT

Write an INSERT transaction: BEGIN, INSERT, COMMIT.

Starter code

query.sql
-- write transaction

Sample solution

query.sql
BEGIN;
INSERT INTO orders (id, amount) VALUES (1, 50);
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;
INSERT INTO orders (id, amount) VALUES (1, 50);
____;
Answer
BEGIN;
INSERT INTO orders (id, amount) VALUES (1, 50);
COMMIT;

Explanation: COMMIT makes the changes in the transaction permanent.

2 What does COMMIT do?

  • Deletes them
  • Undoes the changes
  • Starts a transaction
  • Makes the changes permanent Correct answer

Explanation: COMMIT makes changes permanent.

3 BEGIN and COMMIT: match the terms

Term Meaning
BEGIN start
COMMIT commit
permanent not temporary
visible shared

All lessons in Intermediate