BEGIN and COMMIT
- SQL course
- Intermediate
- Lesson 101/115
- 4 min
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
-- write transactionSample solution
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
BEGIN;
INSERT INTO orders (id, amount) VALUES (1, 50);
____;
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 |