What if you could undo mistakes in your database as easily as pressing Ctrl+Z?
Why COMMIT and ROLLBACK behavior in SQL? - Purpose & Use Cases
Imagine you are updating a long list of customer orders in a notebook. If you make a mistake halfway, you have to erase everything manually or start over. There is no easy way to undo just the last few changes.
Manually tracking changes is slow and risky. You might miss errors or accidentally keep wrong data. Fixing mistakes means rewriting or erasing many pages, which wastes time and causes frustration.
COMMIT and ROLLBACK let you control when changes become permanent or get undone. You can try updates safely, and if something goes wrong, just ROLLBACK to the previous correct state without hassle.
Update orders one by one and rewrite if error found
BEGIN TRANSACTION; UPDATE orders SET status='shipped'; -- if error ROLLBACK; else COMMIT;
This behavior lets you make safe, reversible changes to your data, avoiding costly mistakes and keeping your database reliable.
When booking a flight, the system updates seat availability and payment info together. If payment fails, ROLLBACK cancels the seat hold, preventing errors.
Manual updates are error-prone and hard to fix.
COMMIT saves changes permanently.
ROLLBACK undoes changes to keep data safe.
Practice
COMMIT command do in a database transaction?Solution
Step 1: Understand the purpose of COMMIT
The COMMIT command finalizes all changes made in the current transaction and saves them permanently in the database.Step 2: Compare with other options
ROLLBACK undoes changes, starting a transaction is done by BEGIN or START TRANSACTION, and locking is a different operation.Final Answer:
It saves all changes made during the transaction permanently. -> Option DQuick Check:
COMMIT = Save changes permanently [OK]
- Confusing COMMIT with ROLLBACK
- Thinking COMMIT starts a transaction
- Assuming COMMIT locks the database
Solution
Step 1: Identify the command to undo changes
The ROLLBACK command is used to undo all changes made since the last COMMIT.Step 2: Check syntax correctness
ROLLBACK; is the correct syntax. COMMIT; saves changes, UNDO; and SAVE; are not valid SQL commands for this purpose.Final Answer:
ROLLBACK; -> Option BQuick Check:
ROLLBACK = Undo changes [OK]
- Using COMMIT to undo changes
- Assuming UNDO is a valid SQL command
- Confusing SAVE with COMMIT
BEGIN TRANSACTION;
INSERT INTO users (id, name) VALUES (1, 'Alice');
ROLLBACK;
SELECT * FROM users WHERE id = 1;
What will the SELECT query return?
Solution
Step 1: Analyze the transaction commands
The INSERT adds a row with id=1 and name='Alice', but then ROLLBACK undoes this change.Step 2: Understand the effect of ROLLBACK on SELECT
Since the insert was rolled back, the row does not exist. The SELECT query will find no rows with id=1.Final Answer:
No rows -> Option AQuick Check:
ROLLBACK removes inserted rows [OK]
- Assuming inserted row remains after ROLLBACK
- Thinking ROLLBACK affects only future queries
- Confusing ROLLBACK with COMMIT
BEGIN TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 5;
COMMIT;
ROLLBACK;
What is the effect of the final ROLLBACK?
Solution
Step 1: Understand COMMIT effect
COMMIT saves all changes permanently, so the UPDATE is finalized.Step 2: Analyze ROLLBACK after COMMIT
ROLLBACK after COMMIT does nothing because there is no active transaction to undo.Final Answer:
It has no effect because changes were already committed. -> Option CQuick Check:
ROLLBACK after COMMIT = no effect [OK]
- Thinking ROLLBACK undoes committed changes
- Assuming ROLLBACK causes error after COMMIT
- Confusing order of commands
1. BEGIN TRANSACTION;
2. INSERT INTO orders VALUES (101, 'Book');
3. INSERT INTO orders VALUES (102, 'Pen');
4. -- If both inserts succeed, then ___;
5. -- If any insert fails, then ___;
Solution
Step 1: Understand transaction control
To save changes only if all inserts succeed, you COMMIT after success and ROLLBACK if any fail.Step 2: Match commands to conditions
Line 4 should be COMMIT to save changes; line 5 should be ROLLBACK to undo partial changes.Final Answer:
4: COMMIT; 5: ROLLBACK; -> Option AQuick Check:
COMMIT on success, ROLLBACK on failure [OK]
- Swapping COMMIT and ROLLBACK roles
- Committing even if inserts fail
- Rolling back after successful inserts
Ready to prove it?
Pass the test and this pattern moves unknown
