MySQL Transactions — COMMIT & ROLLBACK
MYSQL · TRANSACTIONS

COMMIT & ROLLBACK

A short, practical guide to controlling changes made inside a database transaction.

1. The basic idea

A transaction is a group of SQL operations treated as one unit of work.

COMMIT permanently saves the changes made in the current transaction. ROLLBACK cancels the uncommitted changes and restores the data to its state at the start of that transaction (or the latest savepoint).

Remember: COMMIT = keep the changes. ROLLBACK = undo the uncommitted changes.

2. How to use them

COMMIT — save the transaction

START TRANSACTION;

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

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

COMMIT;

Both updates are saved together when COMMIT runs.

ROLLBACK — undo the transaction

START TRANSACTION;

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

-- Suppose we discover an error:
ROLLBACK;

The balance change is undone because the transaction was not committed.

SAVEPOINT — undo only part of a transaction

START TRANSACTION;

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

SAVEPOINT after_debit;

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

-- Undo changes after the savepoint:
ROLLBACK TO SAVEPOINT after_debit;

-- Finish the transaction:
COMMIT;

ROLLBACK TO SAVEPOINT undoes work after that savepoint, but keeps the earlier work in the transaction.

Important: Use a transactional storage engine such as InnoDB. These examples assume autocommit is enabled by default, so explicitly start a transaction with START TRANSACTION. DDL statements such as CREATE TABLE and DROP TABLE can cause implicit commits in MySQL; practise rollback with INSERT, UPDATE and DELETE.

3. Practice schema

Run this setup once. It creates two tables and inserts sample records.

CREATE DATABASE transaction_practice;
USE transaction_practice;

CREATE TABLE accounts (
    account_id INT PRIMARY KEY,
    holder_name VARCHAR(50),
    balance DECIMAL(10,2)
) ENGINE=InnoDB;

CREATE TABLE transactions (
    txn_id INT PRIMARY KEY,
    account_id INT,
    amount DECIMAL(10,2),
    txn_type VARCHAR(10),
    FOREIGN KEY (account_id) REFERENCES accounts(account_id)
) ENGINE=InnoDB;

INSERT INTO accounts VALUES
(1, 'Asha', 5000.00),
(2, 'Ravi', 3000.00),
(3, 'Maya', 2500.00);

INSERT INTO transactions VALUES
(101, 1, 500.00, 'CREDIT'),
(102, 2, 200.00, 'DEBIT');

Initial data

SELECT * FROM accounts;

+------------+-------------+---------+
| account_id | holder_name | balance |
+------------+-------------+---------+
| 1          | Asha        | 5000.00 |
| 2          | Ravi        | 3000.00 |
| 3          | Maya        | 2500.00 |
+------------+-------------+---------+

4. Try these questions

Use the schema above. Predict the result before running each query.

  1. Start a transaction. Deduct ₹500 from Asha and add ₹500 to Ravi. Commit the transaction. What are their final balances?
  2. Start a transaction. Add ₹1,000 to Maya's balance, then rollback. What is Maya's balance?
  3. Start a transaction. Deduct ₹200 from Asha. Create a savepoint named s1. Add ₹700 to Ravi. Roll back to s1, then commit. Which changes remain?
  4. Start a transaction. Insert a new account for account_id 4, then rollback. Does the new account exist?
  5. Start a transaction. Delete Ravi's account, then rollback. Is Ravi's account restored?
  6. Start a transaction. Update Asha's balance to 9000. Commit. Then issue ROLLBACK. Does it restore the old balance? Explain.
  7. Without running the code, explain why a bank transfer should debit one account and credit another inside the same transaction.

5. Check your understanding

Question 1 — answer

Asha: ₹4,500. Ravi: ₹3,500. Both updates are saved by COMMIT.

Question 2 — answer

Maya remains at ₹2,500. ROLLBACK cancels the uncommitted update.

Question 3 — answer

Asha's ₹200 deduction remains and is committed. Ravi's ₹700 addition is undone because it happened after savepoint s1.

Question 4 — answer

No. The INSERT is undone by ROLLBACK.

Question 5 — answer

Yes. The DELETE is undone, so Ravi's account is restored.

Question 6 — answer

No. Once COMMIT succeeds, a later ROLLBACK cannot undo that committed transaction. A new UPDATE is needed to change the value back.

Question 7 — answer

The transaction ensures both operations succeed together or neither takes effect. This prevents money being deducted without being credited to the recipient.