Transaction Control: START TRANSACTION, COMMIT, ROLLBACK
Transaction Control: START TRANSACTION, COMMIT, ROLLBACK
By default, MySQL operates in autocommit mode. In autocommit mode, every individual SQL statement (INSERT, UPDATE, DELETE) is treated as an independent transaction that is committed immediately upon execution.
To bundle multiple SQL statements into a single atomic unit, you must use Transaction Control Language (TCL) commands: START TRANSACTION, COMMIT, and ROLLBACK.
Understanding the AUTOCOMMIT Variable
In MySQL, the autocommit session variable dictates transaction boundaries:
When autocommit = 1 (default), issuing START TRANSACTION temporarily disables autocommit until you explicitly issue COMMIT or ROLLBACK.
Transaction Lifecycle Syntax
Practical Walkthrough: Safe E-Commerce Checkout
Consider a checkout workflow that must deduct product stock and record the purchase order atomically:
If any step fails (e.g., out-of-stock check fails or invalid customer ID), executing ROLLBACK; cleanly resets the inventory and prevents partial orders from being recorded.
Implicit Commits: DDL Statements You Must Avoid Inside Transactions!
A critical architectural pitfall in MySQL is that Data Definition Language (DDL) statements trigger an implicit commit!
If you issue any of the following statements inside an open transaction, MySQL automatically commits all preceding statements immediately:
CREATE TABLE,ALTER TABLE,DROP TABLECREATE INDEX,DROP INDEXTRUNCATE TABLECREATE DATABASE,DROP DATABASELOCK TABLES,UNLOCK TABLES
Multiple Choice Questions
1. What does the COMMIT statement do?
A. Temporarily saves rows in the buffer cache B. Permanently applies all modifications made during the transaction to disk C. Clears table data and resets auto-increment keys D. Closes the MySQL server connection Answer: B Explanation: COMMIT makes all updates made within the current transaction permanent and visible to all other database sessions.
2. What happens to uncommitted changes if ROLLBACK is executed?
A. They are saved into a temporary archive table B. All modifications made since START TRANSACTION are canceled and reverted C. The changes are scheduled for execution tomorrow D. Only the last INSERT is reversed Answer: B Explanation: ROLLBACK cancels all modifications executed within the active transaction, restoring the database to the state prior to START TRANSACTION.
3. In MySQL's default state, what is the value of @@autocommit?
A. 0 (OFF) B. 1 (ON) C. -1 (DISABLED) D. NULL Answer: B Explanation: By default, MySQL sets autocommit = 1, meaning every single individual statement is automatically committed unless an explicit START TRANSACTION is initiated.
4. Which of the following statements triggers an implicit COMMIT in MySQL, preventing subsequent rollbacks?
A. SELECT FROM customers; B. ALTER TABLE orders ADD COLUMN notes TEXT; C. UPDATE products SET price = 10; D. INSERT INTO log_table VALUES ('test'); Answer: B Explanation:* DDL commands (such as ALTER TABLE, CREATE TABLE, TRUNCATE) trigger an immediate implicit commit in MySQL.
5. What is the synonym for START TRANSACTION in MySQL?
A. INITIATE B. BEGIN C. OPEN D. RUN Answer: B Explanation: The command BEGIN (or BEGIN WORK) is fully supported as an alias for START TRANSACTION in MySQL.
Using SAVEPOINT & ROLLBACK TO SAVEPOINT
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| ACID Properties in Relational Databases | Using SAVEPOINT & ROLLBACK TO SAVEPOINT |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.