Lesson 57 of 60 – COMMIT & ROLLBACK
95%

COMMIT & ROLLBACK

COMMIT and ROLLBACK are transaction control commands used to manage changes made inside a database transaction. COMMIT saves the changes, while ROLLBACK cancels uncommitted changes.

Note: COMMIT and ROLLBACK are especially useful when several SQL operations must be treated as one logical unit of work.

1. What is COMMIT?

COMMIT permanently saves the changes made during the current transaction.

START TRANSACTION;

UPDATE students
SET paid_fee = paid_fee + 1000
WHERE id = 1;

COMMIT;

After COMMIT, the transaction is successfully completed.

2. What is ROLLBACK?

ROLLBACK cancels changes made during the current transaction that have not been committed.

START TRANSACTION;

UPDATE students
SET paid_fee = paid_fee + 1000
WHERE id = 1;

ROLLBACK;

The uncommitted UPDATE is undone.

3. START TRANSACTION

Before using COMMIT or ROLLBACK for a group of operations, an explicit transaction can be started.

START TRANSACTION;

INSERT INTO payments
(student_id, amount)
VALUES
(101, 2000);

COMMIT;

The INSERT and COMMIT belong to the same transaction.

4. BEGIN Transaction

BEGIN can also be used to start a transaction in MySQL.

BEGIN;

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

COMMIT;

The transaction can then be committed or rolled back.

5. Basic COMMIT Example

Suppose a student pays ₹2,000.

START TRANSACTION;

UPDATE students
SET paid_fee = paid_fee + 2000
WHERE id = 101;

COMMIT;

COMMIT saves the update as part of the transaction.

6. Basic ROLLBACK Example

If the update should not be saved, use ROLLBACK before the transaction is committed.

START TRANSACTION;

UPDATE students
SET paid_fee = paid_fee + 2000
WHERE id = 101;

ROLLBACK;

The uncommitted change is cancelled.

7. COMMIT with INSERT

COMMIT can save INSERT operations.

START TRANSACTION;

INSERT INTO students
(name, city)
VALUES
('Rahul', 'Patna');

COMMIT;

The new student record is committed.

8. ROLLBACK with INSERT

An INSERT can be undone before it is committed.

START TRANSACTION;

INSERT INTO students
(name, city)
VALUES
('Rahul', 'Patna');

ROLLBACK;

The uncommitted INSERT is cancelled.

9. COMMIT with UPDATE

COMMIT can permanently save an UPDATE.

START TRANSACTION;

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

COMMIT;

The status change is committed.

10. ROLLBACK with UPDATE

If an UPDATE was made accidentally, it can be rolled back before COMMIT.

START TRANSACTION;

UPDATE students
SET status = 'Inactive'
WHERE id = 10;

ROLLBACK;

The uncommitted update is cancelled.

11. COMMIT with DELETE

DELETE operations can also be committed.

START TRANSACTION;

DELETE FROM students
WHERE id = 10;

COMMIT;

The DELETE becomes committed according to the transaction rules.

12. ROLLBACK with DELETE

A DELETE can be cancelled before COMMIT.

START TRANSACTION;

DELETE FROM students
WHERE id = 10;

ROLLBACK;

The uncommitted DELETE is undone.

13. COMMIT Multiple Statements

Multiple SQL statements can be committed together.

START TRANSACTION;

UPDATE students
SET paid_fee = paid_fee + 1000
WHERE id = 1;

INSERT INTO payments
(student_id, amount)
VALUES
(1, 1000);

COMMIT;

The statements are handled as one transaction.

14. ROLLBACK Multiple Statements

If a transaction contains multiple uncommitted changes, ROLLBACK can cancel them.

START TRANSACTION;

UPDATE students
SET paid_fee = paid_fee + 1000
WHERE id = 1;

INSERT INTO payments
(student_id, amount)
VALUES
(1, 1000);

ROLLBACK;

The uncommitted changes from the transaction are rolled back.

15. COMMIT after SAVEPOINT

A transaction can use a SAVEPOINT and then be committed.

START TRANSACTION;

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

SAVEPOINT point1;

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

COMMIT;

COMMIT completes the transaction and releases its transaction state.

16. ROLLBACK TO SAVEPOINT

You can roll back to a specific savepoint without rolling back the entire transaction.

START TRANSACTION;

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

SAVEPOINT point1;

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

ROLLBACK TO SAVEPOINT point1;

COMMIT;

The changes made after the savepoint are rolled back, while earlier changes can remain in the transaction.

17. RELEASE SAVEPOINT

Once a savepoint is no longer needed, it can be released.

START TRANSACTION;

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

SAVEPOINT point1;

RELEASE SAVEPOINT point1;

COMMIT;

Releasing a savepoint does not commit the transaction.

18. COMMIT after Successful Validation

Applications can perform validation before committing a transaction.

START TRANSACTION;

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

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

-- If validation succeeds
COMMIT;

The application should commit only when the complete business operation is valid.

19. ROLLBACK after an Error

If an important operation fails, the application can roll back the transaction.

START TRANSACTION;

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

-- Some required operation fails

ROLLBACK;

The uncommitted changes are cancelled.

20. COMMIT and ROLLBACK in Payment System

A payment system may need to insert a payment and update the student's paid amount together.

START TRANSACTION;

INSERT INTO payments
(student_id, amount)
VALUES
(101, 2000);

UPDATE students
SET paid_fee = paid_fee + 2000
WHERE id = 101;

COMMIT;

If the operation cannot be completed correctly, the application can use ROLLBACK.

21. COMMIT and Autocommit

MySQL commonly uses autocommit by default. When autocommit is enabled, individual statements are automatically committed unless an explicit transaction is started.

START TRANSACTION;

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

COMMIT;

Explicit transaction boundaries are useful when multiple statements must be handled together.

22. COMMIT Cannot Be Undone by ROLLBACK

Once a transaction has been successfully committed, a later ROLLBACK does not undo that already committed transaction.

START TRANSACTION;

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

COMMIT;

ROLLBACK;

The ROLLBACK does not reverse the changes that were already committed.

23. COMMIT and ROLLBACK with Bank Transfer

A bank transfer can contain two related balance updates.

START TRANSACTION;

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

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

COMMIT;

If the operation cannot be completed correctly before COMMIT, the application can roll back the transaction.

24. COMMIT and ROLLBACK with Orders

An online order can involve multiple operations.

START TRANSACTION;

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

UPDATE products
SET quantity = quantity - 1
WHERE id = 20;

COMMIT;

If a required operation fails before COMMIT, ROLLBACK can cancel the uncommitted changes.

25. COMMIT and ROLLBACK with Inventory

Inventory updates can be grouped into a transaction.

START TRANSACTION;

UPDATE products
SET quantity = quantity - 5
WHERE id = 101;

UPDATE stock
SET available = available - 5
WHERE product_id = 101;

COMMIT;

Both updates can be handled together by the application.

26. Common COMMIT Mistakes

  • Committing before all required operations are complete.
  • Forgetting to handle application errors.
  • Keeping transactions open unnecessarily long.
  • Assuming ROLLBACK can undo an already committed transaction.
  • Not checking whether the database engine supports the required transaction behavior.

27. Common ROLLBACK Mistakes

  • Using ROLLBACK after COMMIT and expecting committed changes to disappear.
  • Calling ROLLBACK when no relevant transaction is active.
  • Forgetting to start an explicit transaction when multiple statements must be grouped.
  • Using an unsupported storage engine for the required transaction behavior.

28. COMMIT vs ROLLBACK

COMMIT ROLLBACK
Saves the transaction's changes Cancels uncommitted transaction changes
Completes the transaction successfully Abandons the uncommitted changes
Used when operations are successful Used when operations should not be kept

29. Practical Student Fee Example

Suppose a student pays ₹3,000. The system needs to create a payment record and update the student's paid fee.

START TRANSACTION;

INSERT INTO payments
(student_id, amount)
VALUES
(101, 3000);

UPDATE students
SET paid_fee = paid_fee + 3000
WHERE id = 101;

COMMIT;

If the application detects a problem before COMMIT:

ROLLBACK;

The transaction can be cancelled instead of keeping the uncommitted changes.

30. Complete COMMIT & ROLLBACK Example

Consider a student fee payment system where payment insertion and fee update must be handled together.

START TRANSACTION;

INSERT INTO payments
(student_id, amount)
VALUES
(101, 2000);

UPDATE students
SET paid_fee = paid_fee + 2000
WHERE id = 101;

-- Check whether all operations were successful

COMMIT;

If an error occurs before COMMIT:

ROLLBACK;

For a more detailed transaction, a savepoint can be used:

START TRANSACTION;

INSERT INTO payments
(student_id, amount)
VALUES
(101, 2000);

SAVEPOINT payment_point;

UPDATE students
SET paid_fee = paid_fee + 2000
WHERE id = 101;

COMMIT;

This demonstrates the basic transaction flow: START TRANSACTION → perform operations → COMMIT if successful → ROLLBACK if the transaction must be cancelled.

📌 Key Points

  • COMMIT saves the changes made during a transaction.
  • ROLLBACK cancels uncommitted changes.
  • START TRANSACTION begins an explicit transaction.
  • BEGIN can also start a transaction in MySQL.
  • COMMIT cannot be used to undo an already committed transaction.
  • ROLLBACK is useful when an operation fails before COMMIT.
  • SAVEPOINT creates a point inside a transaction.
  • ROLLBACK TO SAVEPOINT reverses changes after a specific savepoint.
  • COMMIT and ROLLBACK are commonly used in payment, order and banking systems.
  • Transactions should be kept focused and should not remain open unnecessarily.

🧠 Quick Quiz

Question: Which command cancels uncommitted changes in a transaction?