Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
MySQL + SQL · Lesson 85

COMMIT, ROLLBACK and SAVEPOINT

COMMIT, ROLLBACK and SAVEPOINT

These transaction control statements decide how much active work is kept. COMMIT accepts the complete transaction, ROLLBACK rejects all its pending work, and a savepoint creates a named position for partial recovery without ending the transaction.

StatementEffectTransaction remains active?
COMMITMakes current changes permanentNo
ROLLBACKUndoes all current uncommitted changesNo
SAVEPOINT spCreates or replaces named marker spYes
ROLLBACK TO spUndoes changes after spYes
RELEASE SAVEPOINT spDeletes marker sp onlyYes
Key distinction: ROLLBACK ends the transaction; ROLLBACK TO SAVEPOINT does not.

Verified Student-Result Lab

Run the DDL and seed rows before starting transaction experiments. InnoDB is essential for predictable rollback behavior.

DROP TABLE IF EXISTS student_results;

CREATE TABLE student_results (
  student_id INT PRIMARY KEY,
  student_name VARCHAR(60) NOT NULL,
  marks DECIMAL(5,2) NOT NULL,
  CONSTRAINT chk_marks CHECK (marks BETWEEN 0 AND 100)
) ENGINE = InnoDB;

INSERT INTO student_results VALUES
(1, 'Aarav', 80.00),
(2, 'Meera', 85.00),
(3, 'Sana',  90.00);
1 | Aarav | 80.00 2 | Meera | 85.00 3 | Sana | 90.00

Reset to these seed values before running each independent example, or run the examples in the displayed order and observe every checkpoint.

COMMIT: Permanently Accept the Unit

START TRANSACTION;

UPDATE student_results
SET marks = marks + 5
WHERE student_id = 1;

SELECT student_id, marks
FROM student_results
WHERE student_id = 1;
-- Current session sees 85.00.

COMMIT;

SELECT student_id, marks
FROM student_results
WHERE student_id = 1;
-- 85.00 is now committed.
Before transaction: 80.00 Before COMMIT: 85.00 After COMMIT: 85.00

COMMIT ends the current transaction, deletes its savepoints and releases its InnoDB locks. After a successful commit, a later ROLLBACK cannot travel back into that completed transaction.

Full ROLLBACK: Reject Every Pending Change

This example temporarily changes two rows, checks the session's provisional view, then cancels both changes.

START TRANSACTION;

UPDATE student_results
SET marks = 40.00
WHERE student_id = 2;

UPDATE student_results
SET marks = 45.00
WHERE student_id = 3;

SELECT student_id, marks
FROM student_results
WHERE student_id IN (2, 3)
ORDER BY student_id;
-- Current session: Meera=40.00, Sana=45.00.

ROLLBACK;

SELECT student_id, marks
FROM student_results
WHERE student_id IN (2, 3)
ORDER BY student_id;
After ROLLBACK: Meera=85.00, Sana=90.00

A single statement error does not universally mean that MySQL has rolled back the entire transaction. Application code must detect failure and issue full ROLLBACK unless the complete transaction is already known to have ended.

SAVEPOINT: Correct Only the Later Work

Assume Aarav's and Meera's updates are correct, but Sana was accidentally changed to 50 instead of 92. Savepoints let us preserve the earlier valid work.

START TRANSACTION;

UPDATE student_results SET marks = 85.00
WHERE student_id = 1;
SAVEPOINT after_aarav;

UPDATE student_results SET marks = 95.00
WHERE student_id = 2;
SAVEPOINT after_meera;

UPDATE student_results SET marks = 50.00
WHERE student_id = 3;  -- logical mistake

ROLLBACK TO SAVEPOINT after_meera;
-- Sana returns to 90.00; earlier updates remain pending.

UPDATE student_results SET marks = 92.00
WHERE student_id = 3;

RELEASE SAVEPOINT after_meera;
COMMIT;
Final committed result 1 | Aarav | 85.00 2 | Meera | 95.00 3 | Sana | 92.00

ROLLBACK TO undid only the mistaken Sana update. It did not commit Aarav or Meera, and it did not end the transaction. The final COMMIT accepted all three corrected results.

Exact SAVEPOINT Rules in MySQL

  • A savepoint exists only inside the current transaction.
  • Creating a savepoint with an existing name deletes the old marker and sets the new one.
  • ROLLBACK TO the named savepoint keeps that named point but deletes savepoints created after it.
  • ROLLBACK TO undoes row modifications after the marker. InnoDB does not necessarily release every row lock stored in memory after that marker.
  • RELEASE SAVEPOINT removes only the marker; it does not change table data.
  • COMMIT or full ROLLBACK deletes all savepoints for the transaction.
  • Using an unknown savepoint name returns error 1305.
START TRANSACTION;
SAVEPOINT review_point;
UPDATE student_results SET marks = 86 WHERE student_id = 1;
SAVEPOINT review_point;
-- The first review_point was replaced by the second.
ROLLBACK TO review_point;
COMMIT;

Use descriptive names such as after_header or before_optional_items. Savepoints are recovery markers, not a substitute for validating every statement.

Autocommit, DDL and Other Traps

With autocommit enabled and no explicit transaction, a successful UPDATE commits immediately. A savepoint therefore requires an active transaction.

SELECT @@autocommit;  -- normally 1

START TRANSACTION;
UPDATE student_results SET marks = 88 WHERE student_id = 1;
SAVEPOINT after_first_update;
-- Continue, roll back to the marker, or finish explicitly.
Implicit-commit trap: statements such as CREATE TABLE, ALTER TABLE, DROP TABLE and TRUNCATE TABLE can end the current transaction. Do not place schema changes between SAVEPOINT and ROLLBACK TO.
  • Do not leave an interactive transaction open after a savepoint.
  • Do not assume ROLLBACK can undo already committed work.
  • Do not use nontransactional tables for work that must be recoverable.
  • Do not confuse RELEASE SAVEPOINT with COMMIT.
  • Do not expose savepoint names directly from untrusted user input.

Which Command Should You Use?

SituationCommand
All required statements and checks succeededCOMMIT
A critical step failed and no pending work should remainROLLBACK
Earlier pending work is valid but later optional work is wrongROLLBACK TO SAVEPOINT, correct, then decide COMMIT
A marker is no longer requiredRELEASE SAVEPOINT
Work is already committed but business correction is neededCreate an auditable compensating transaction

Practice tasks: add a fourth student after a savepoint and roll only that insert back; create two savepoints and prove that rolling to the earlier one deletes the later marker; test a full ROLLBACK; then verify final rows from a second database session.

Review the broader transactions lesson and the ACID properties lesson to connect these commands to reliability.

Official References

Command behavior and savepoint lifecycle were checked against the official MySQL 8.4 manual.

Frequently Asked Questions

What is the difference between COMMIT and ROLLBACK in MySQL?
COMMIT ends the current transaction and makes its changes permanent. Full ROLLBACK ends it and cancels its uncommitted modifications. Both release the transaction’s InnoDB locks.
Does ROLLBACK TO SAVEPOINT end the transaction?
No. It undoes changes made after the named savepoint but keeps the transaction active, so you can correct the work and later COMMIT or fully ROLLBACK.
What does RELEASE SAVEPOINT do?
It removes the named marker. It neither commits nor rolls back data, and referring to a nonexistent savepoint produces an error.
What happens to savepoints after COMMIT or full ROLLBACK?
All savepoints of that transaction are deleted because the transaction has ended.
Can SAVEPOINT undo an ALTER TABLE or TRUNCATE TABLE?
Do not rely on it. Many DDL statements cause an implicit commit, which ends the active transaction and removes its savepoints.
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.