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

COMMIT & ROLLBACK

COMMIT is like pressing save on a group of changes for good, and ROLLBACK is like pressing 'undo everything' back to the start.
Syntax
sql
START TRANSACTION;
-- SQL statements
COMMIT;    -- save the changes
-- or
ROLLBACK;  -- undo the changes

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.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Assuming ROLLBACK works after COMMIT, when committed changes are permanent.
  2. Leaving a transaction open for a long time, which holds locks and blocks other users.
  3. Turning autocommit off with SET autocommit = 0 and then forgetting to commit.
🔒

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.