← Back to MySQL Course | Chapter 17: Transactions | Lesson 2 of 4

COMMIT & ROLLBACK

Introduction to Transactions

COMMIT and ROLLBACK are the two ways a transaction can end: COMMIT locks in every change made since START TRANSACTION as permanent, while ROLLBACK throws all of them away as if the transaction never happened.

Example: Introduction to Transactions

sql
START TRANSACTION;
UPDATE accounts SET balance = balance - 50 WHERE id = 1;
COMMIT;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Undoing Changes with ROLLBACK

ROLLBACK is your safety net when something goes wrong mid-transaction — a failed validation, an unexpected error, or a business rule violation — letting you cancel every change made so far and leave the data exactly as it was.

Example: Undoing Changes with ROLLBACK

sql
START TRANSACTION;
UPDATE accounts SET balance = balance - 50 WHERE id = 1;
-- Validation failed -- cancel everything since START TRANSACTION
ROLLBACK;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Auto-Commit Mode in MySQL

Turning off autocommit mode puts you in full manual control of when data becomes permanent, which matters most for multi-step operations like transferring money between two accounts that must both succeed or both fail together.

Example: Auto-Commit Mode in MySQL

sql
SET autocommit = 0;
START TRANSACTION;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Checking Transaction Status

You can check the current autocommit setting to confirm whether MySQL is saving your statements immediately or waiting for an explicit COMMIT, which is worth verifying before running sensitive multi-step logic.

Example: Checking Transaction Status

sql
SELECT @@autocommit AS autocommit_enabled;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Best Practices for Transactions

Keeping transactions short is a real performance concern — a long-running transaction can hold locks on rows or tables for an extended period, blocking other users, so always close it promptly with COMMIT or ROLLBACK.

Example: Best Practices for Transactions

sql
START TRANSACTION;
UPDATE accounts SET balance = balance - 50 WHERE id = 1;
UPDATE accounts SET balance = balance + 50 WHERE id = 2;
COMMIT;  -- close it promptly, don't hold locks longer than needed

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

🔒

Chapter Quiz — Complete all 4 topics to unlock

0/4 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.