LearnContact
Lesson 6019 min read

COMMIT, ROLLBACK & SAVEPOINT

Learn how to permanently save a transaction with COMMIT, undo it with ROLLBACK, and mark partial checkpoints with SAVEPOINT.

Introduction

The previous lesson introduced transactions and showed COMMIT saving a transfer between two accounts. But what happens when something goes wrong partway through? You need a way to undo everything — or sometimes just part of it.

That is exactly what COMMIT, ROLLBACK, and SAVEPOINT give you: precise control over whether a transaction’s changes become permanent, get completely undone, or get partially undone back to a specific checkpoint.

What You Will Learn
  • How COMMIT permanently saves every change in a transaction.
  • How ROLLBACK undoes every change since the transaction began.
  • How SAVEPOINT marks a checkpoint you can return to.
  • How ROLLBACK TO SAVEPOINT undoes only part of a transaction.
  • A worked example continuing the bank transfer, showing a rollback after a simulated error.

COMMIT: Saving a Transaction

COMMIT permanently saves every change made since the transaction began with START TRANSACTION. Once committed, the changes are visible to every other connection and cannot be undone with ROLLBACK.

START TRANSACTION;

UPDATE accounts SET balance = balance - 500 WHERE id = 1;
UPDATE accounts SET balance = balance + 500 WHERE id = 2;

COMMIT;

After COMMIT runs, both UPDATE statements are locked in permanently, exactly as if they had never been part of a transaction at all.

ROLLBACK: Undoing a Transaction

ROLLBACK undoes every change made since the transaction began, restoring the data to exactly how it looked before START TRANSACTION. It is your safety net whenever something inside the transaction goes wrong.

START TRANSACTION;

UPDATE accounts SET balance = balance - 500 WHERE id = 1;
UPDATE accounts SET balance = balance + 500 WHERE id = 2;

-- Something went wrong -- undo everything above
ROLLBACK;

After ROLLBACK, it is as if neither UPDATE statement ever ran. Both account balances remain exactly as they were before the transaction started.

SAVEPOINT: Marking a Checkpoint

Sometimes you do not want to undo an entire transaction — only the most recent part of it. SAVEPOINT lets you mark a named checkpoint inside a transaction that you can later roll back to, without losing everything that came before it.

START TRANSACTION;

UPDATE accounts SET balance = balance - 500 WHERE id = 1;

SAVEPOINT after_debit;

UPDATE accounts SET balance = balance + 500 WHERE id = 2;

At this point, after_debit marks a checkpoint right after the debit from account 1, but before the credit to account 2.

ROLLBACK TO SAVEPOINT

ROLLBACK TO SAVEPOINT undoes only the changes made after that savepoint was created, leaving everything before it intact and the transaction still open.

ROLLBACK TO SAVEPOINT after_debit;

-- The debit from account 1 is still in place.
-- The credit to account 2 has been undone.
-- The transaction is still open -- you can try again or COMMIT/ROLLBACK.
Key Difference

ROLLBACK ends the transaction entirely and undoes everything. ROLLBACK TO SAVEPOINT only undoes changes made after that checkpoint, and the transaction remains open afterward.

RELEASE SAVEPOINT

Once you no longer need a savepoint, you can remove it with RELEASE SAVEPOINT. This does not undo any changes — it simply forgets the checkpoint.

RELEASE SAVEPOINT after_debit;

You rarely need to call this explicitly, since all savepoints are automatically released when the transaction ends with COMMIT or ROLLBACK.

Worked Example: A Failed Transfer

Let’s continue the bank transfer scenario from the previous lesson, but this time simulate an error midway through, and recover from it using a savepoint instead of losing the whole transaction.

setup.sql
CREATE TABLE accounts (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    balance DECIMAL(10,2)
);

INSERT INTO accounts VALUES
    (1, 'Alice', 5000.00),
    (2, 'Bob', 2000.00);
transfer_with_savepoint.sql
START TRANSACTION;

UPDATE accounts SET balance = balance - 1000.00 WHERE id = 1;

SAVEPOINT after_debit;

-- Simulated mistake: crediting a nonexistent account id (99)
UPDATE accounts SET balance = balance + 1000.00 WHERE id = 99;

-- Nothing matched id 99, so the credit never happened -- roll back to the checkpoint
ROLLBACK TO SAVEPOINT after_debit;

-- Retry the credit with the correct account id
UPDATE accounts SET balance = balance + 1000.00 WHERE id = 2;

COMMIT;

SELECT * FROM accounts;
Result
+----+-------+----------+
| id | name  | balance  |
+----+-------+----------+
| 1  | Alice | 4000.00  |
| 2  | Bob   | 3000.00  |
+----+-------+----------+
What Just Happened

The debit from Alice was preserved by the savepoint even after the mistaken credit to account 99 had to be rolled back. The transaction was corrected and completed successfully, ending in the same result as a clean transfer — without needing to redo the debit step.

Common Mistakes

Avoid These Mistakes
  • Confusing ROLLBACK (undoes everything, ends the transaction) with ROLLBACK TO SAVEPOINT (undoes only part, transaction stays open).
  • Forgetting that a plain ROLLBACK after a savepoint still undoes changes made before that savepoint too.
  • Never checking whether a statement actually affected any rows before assuming it succeeded.
  • Leaving a transaction open indefinitely without COMMIT or ROLLBACK, holding locks unnecessarily.
  • Reusing a savepoint name within the same transaction, which simply moves the checkpoint rather than creating a second one.

Best Practices

  • Use SAVEPOINT before any step in a transaction that might fail and needs a safe retry point.
  • Always pair START TRANSACTION with a clear plan for both the COMMIT and ROLLBACK paths.
  • Check the number of affected rows after each statement so a "silent" failure like the id 99 example is caught early.
  • Keep savepoint names descriptive, like after_debit, so a long transaction stays readable.
  • Release or commit savepoints as soon as they are no longer needed, rather than leaving transactions open longer than necessary.

Frequently Asked Questions

Can I create more than one savepoint in a transaction?

Yes, a transaction can have several savepoints, and you can roll back to any of them by name, at any point before COMMIT.

Does ROLLBACK TO SAVEPOINT end the transaction?

No. The transaction remains open after ROLLBACK TO SAVEPOINT, so you can keep working and eventually COMMIT or fully ROLLBACK.

What happens to savepoints after COMMIT or ROLLBACK?

All savepoints are automatically discarded once the transaction ends, whether it ends with COMMIT or a full ROLLBACK.

Is SAVEPOINT available in every MySQL storage engine?

Savepoints require transaction support, so they work with InnoDB but not with non-transactional engines like MyISAM.

Key Takeaways

  • COMMIT permanently saves every change made in the current transaction.
  • ROLLBACK undoes every change made since the transaction began and ends the transaction.
  • SAVEPOINT marks a named checkpoint inside a transaction.
  • ROLLBACK TO SAVEPOINT undoes only the changes made after that checkpoint, leaving the transaction open.
  • RELEASE SAVEPOINT removes a savepoint without undoing any data.

Summary

COMMIT, ROLLBACK, and SAVEPOINT together give you fine-grained control over a transaction’s outcome — saving it permanently, undoing it entirely, or rewinding just part of it after an error.

In this lesson, you learned how COMMIT saves a transaction, how ROLLBACK undoes one, and how SAVEPOINT and ROLLBACK TO SAVEPOINT let you recover from a partial failure without starting over. Next, you will learn about Normalization — how to design tables that avoid redundant, inconsistent data in the first place.

Lesson 60 Completed
  • You can permanently save a transaction with COMMIT.
  • You can fully undo a transaction with ROLLBACK.
  • You can mark and roll back to a checkpoint with SAVEPOINT.
  • You are ready to learn about Normalization.
Next Lesson →

Normalization