Lesson 56 of 60 – COMMIT & ROLLBACK
93%

MySQL COMMIT & ROLLBACK

COMMIT and ROLLBACK are important transaction-control commands in MySQL. COMMIT permanently completes the current transaction, while ROLLBACK cancels uncommitted changes made by the current transaction.

Note: COMMIT and ROLLBACK are mainly useful with transactional storage engines such as InnoDB. Their behavior does not apply identically to every MySQL statement or storage engine.

1. What is COMMIT?

COMMIT is used to successfully complete the current transaction.

COMMIT;

After COMMIT, the transaction's changes become permanent from the transaction's perspective and cannot be undone using ROLLBACK.

2. What is ROLLBACK?

ROLLBACK is used to undo changes made by the current transaction that have not yet been committed.

ROLLBACK;

For a transactional table such as an InnoDB table, changes made since the transaction began can be undone before COMMIT.

3. COMMIT Syntax

The basic syntax is:

COMMIT;

You can also write:

COMMIT WORK;

For normal MySQL usage, COMMIT; is the commonly used form.

4. ROLLBACK Syntax

The basic syntax is:

ROLLBACK;

It cancels the current transaction's uncommitted changes.

5. Starting a Transaction

Before using COMMIT or ROLLBACK explicitly, you can start a transaction using:

START TRANSACTION;

Example:

START TRANSACTION;

UPDATE students
SET status = 'Active'
WHERE student_id = 10;

COMMIT;

6. Simple COMMIT Example

START TRANSACTION;

UPDATE students
SET city = 'Patna'
WHERE student_id = 1;

COMMIT;

The UPDATE is successfully completed and committed.

7. Simple ROLLBACK Example

START TRANSACTION;

UPDATE students
SET city = 'Delhi'
WHERE student_id = 1;

ROLLBACK;

The UPDATE is undone because the transaction was rolled back before it was committed.

8. COMMIT After INSERT

COMMIT can be used after an INSERT:

START TRANSACTION;

INSERT INTO students
(name, email, city)
VALUES
('Amit Kumar', 'amit@example.com', 'Patna');

COMMIT;

The new row is committed as part of the transaction.

9. ROLLBACK After INSERT

START TRANSACTION;

INSERT INTO students
(name, email, city)
VALUES
('Rahul Sharma', 'rahul@example.com', 'Gaya');

ROLLBACK;

For an InnoDB table, the inserted row is removed by the rollback because the INSERT was not committed.

10. COMMIT After UPDATE

START TRANSACTION;

UPDATE students
SET status = 'Active'
WHERE student_id = 5;

COMMIT;

The UPDATE becomes part of the committed transaction.

11. ROLLBACK After UPDATE

START TRANSACTION;

UPDATE students
SET status = 'Inactive'
WHERE student_id = 5;

ROLLBACK;

The UPDATE is undone if the table is transactional and the change has not been committed.

12. COMMIT After DELETE

START TRANSACTION;

DELETE FROM students
WHERE student_id = 20;

COMMIT;

The DELETE is committed.

After COMMIT, you cannot use ROLLBACK to restore the deleted row through that transaction.

13. ROLLBACK After DELETE

START TRANSACTION;

DELETE FROM students
WHERE student_id = 20;

ROLLBACK;

For an InnoDB table, the DELETE is undone because the transaction was rolled back before COMMIT.

14. COMMIT with Multiple Statements

A transaction can contain multiple SQL statements.

START TRANSACTION;

INSERT INTO students
(name, email)
VALUES
('Amit', 'amit@example.com');

UPDATE students
SET status = 'Active'
WHERE student_id = 2;

DELETE FROM students
WHERE student_id = 5;

COMMIT;

All three operations are part of the transaction.

15. ROLLBACK Multiple Statements

If multiple changes belong to the same transaction, ROLLBACK can undo them together.

START TRANSACTION;

INSERT INTO students
(name)
VALUES
('Amit');

UPDATE students
SET status = 'Active'
WHERE student_id = 2;

DELETE FROM students
WHERE student_id = 5;

ROLLBACK;

For transactional tables, the uncommitted changes are rolled back.

16. COMMIT Cannot Be Undone by ROLLBACK

Once a transaction has been committed, ROLLBACK cannot undo that transaction.

START TRANSACTION;

UPDATE students
SET status = 'Active'
WHERE student_id = 1;

COMMIT;

ROLLBACK;

The ROLLBACK after COMMIT does not undo the already committed UPDATE.

Remember: ROLLBACK must be used before COMMIT if you want to cancel the current transaction's changes.

17. COMMIT and Autocommit

MySQL normally has autocommit enabled.

SELECT @@autocommit;

If the result is 1, autocommit is enabled.

With autocommit enabled, individual transactional statements are normally committed automatically unless they are part of an explicit transaction.

18. Disabling Autocommit

You can disable autocommit for the current session:

SET autocommit = 0;

Now you can control when changes are committed:

UPDATE students
SET city = 'Patna'
WHERE student_id = 1;

COMMIT;

Or cancel them:

ROLLBACK;

19. Enabling Autocommit

Autocommit can be enabled again:

SET autocommit = 1;

When autocommit is enabled, each suitable transactional statement is normally committed automatically when it completes successfully outside an explicit transaction.

20. COMMIT with SAVEPOINT

SAVEPOINT allows partial rollback inside a transaction.

START TRANSACTION;

UPDATE students
SET status = 'Active'
WHERE student_id = 1;

SAVEPOINT student_step;

UPDATE students
SET status = 'Inactive'
WHERE student_id = 2;

ROLLBACK TO SAVEPOINT student_step;

COMMIT;

The second UPDATE is rolled back, while the first UPDATE can remain part of the committed transaction.

21. ROLLBACK TO SAVEPOINT

ROLLBACK TO SAVEPOINT does not end the entire transaction.

START TRANSACTION;

UPDATE students
SET city = 'Patna'
WHERE student_id = 1;

SAVEPOINT city_change;

UPDATE students
SET city = 'Delhi'
WHERE student_id = 2;

ROLLBACK TO SAVEPOINT city_change;

UPDATE students
SET city = 'Gaya'
WHERE student_id = 3;

COMMIT;

The transaction continues after the partial rollback.

22. RELEASE SAVEPOINT

A savepoint can be removed using:

RELEASE SAVEPOINT city_change;

After a savepoint is released, you cannot roll back to that savepoint.

23. COMMIT in a Bank Transfer

Suppose ₹1,000 is transferred from account 1 to account 2.

START TRANSACTION;

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

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

COMMIT;

COMMIT completes both related operations as one transaction.

24. ROLLBACK in a Bank Transfer

If a problem occurs before COMMIT, the application can roll back the transaction.

START TRANSACTION;

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

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

ROLLBACK;

For InnoDB, both uncommitted balance changes are undone.

25. COMMIT in an Order System

An order system may insert an order and update inventory within the same transaction.

START TRANSACTION;

INSERT INTO orders
(customer_id, total_amount)
VALUES
(101, 2500);

UPDATE products
SET stock = stock - 1
WHERE product_id = 10;

COMMIT;

The application can commit both operations after verifying that they completed successfully.

26. ROLLBACK in an Order System

If an error occurs during an order transaction, the application may roll back the uncommitted changes.

START TRANSACTION;

INSERT INTO orders
(customer_id, total_amount)
VALUES
(101, 2500);

UPDATE products
SET stock = stock - 1
WHERE product_id = 10;

ROLLBACK;

For InnoDB tables, the transaction's uncommitted changes can be undone.

27. Important Difference Between COMMIT and ROLLBACK

COMMIT ROLLBACK
Completes the transaction Cancels uncommitted transaction changes
Changes become permanent from the transaction perspective Changes are undone for transactional tables
Ends the current transaction Ends the current transaction
Cannot be reversed using ROLLBACK Used before COMMIT to cancel changes

28. Common COMMIT and ROLLBACK Mistakes

  • Calling COMMIT before checking whether all operations succeeded.
  • Forgetting to ROLLBACK after an application error.
  • Assuming ROLLBACK can undo already committed changes.
  • Ignoring autocommit settings.
  • Using non-transactional storage engines and expecting normal rollback behavior.
  • Keeping transactions open longer than necessary.
  • Not handling database errors correctly in application code.

29. COMMIT vs ROLLBACK Example

Successful operation:

START TRANSACTION;

UPDATE students
SET fee_paid = fee_paid + 1000
WHERE student_id = 1;

COMMIT;

Cancel operation:

START TRANSACTION;

UPDATE students
SET fee_paid = fee_paid + 1000
WHERE student_id = 1;

ROLLBACK;

In the first example, the change is committed. In the second example, the uncommitted change is cancelled for a transactional table.

30. Complete COMMIT & ROLLBACK Example

CREATE TABLE accounts (
    account_id INT PRIMARY KEY,
    account_name VARCHAR(100),
    balance DECIMAL(12,2)
) ENGINE=InnoDB;

INSERT INTO accounts
(account_id, account_name, balance)
VALUES
(1, 'Amit', 10000.00),
(2, 'Rahul', 5000.00);

-- Start transaction
START TRANSACTION;

-- Deduct from Amit
UPDATE accounts
SET balance = balance - 1000
WHERE account_id = 1;

-- Add to Rahul
UPDATE accounts
SET balance = balance + 1000
WHERE account_id = 2;

-- Complete transaction
COMMIT;

-- Check balances
SELECT *
FROM accounts;

If an error occurs before COMMIT, use:

ROLLBACK;

For example:

START TRANSACTION;

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

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

-- Cancel both uncommitted changes
ROLLBACK;

This example demonstrates the practical difference between COMMIT and ROLLBACK.

📌 Key Points

  • COMMIT completes the current transaction.
  • ROLLBACK cancels uncommitted changes.
  • COMMIT cannot be used to undo changes after they are committed.
  • ROLLBACK must be performed before COMMIT to cancel the current transaction.
  • START TRANSACTION begins an explicit transaction.
  • BEGIN can also start a transaction.
  • SAVEPOINT allows partial rollback inside a transaction.
  • ROLLBACK TO SAVEPOINT does not end the entire transaction.
  • RELEASE SAVEPOINT removes a savepoint.
  • Autocommit is normally enabled by default.
  • InnoDB supports transactional COMMIT and ROLLBACK behavior.
  • Transactions are useful for banking, payments, orders, fees, and inventory systems.
  • Application code should handle errors and decide whether to COMMIT or ROLLBACK.
  • DDL statements such as CREATE, ALTER, DROP, and TRUNCATE have different transaction behavior from ordinary DML statements.
  • Always understand the storage engine and statement behavior before relying on ROLLBACK.

🧠 Quick Quiz

Question: Which command is used to undo uncommitted changes in a transaction?