β©οΈ The ROLLBACK statement is used to undo all uncommitted changes made during the current transaction. If an error occurs or a transaction cannot be completed successfully, ROLLBACK restores the database to the state it was in before the transaction began.
π What is ROLLBACK?
A transaction groups multiple SQL statements into a single logical unit of work. If any statement within the transaction fails, the ROLLBACK statement cancels all changes made since the transaction started, ensuring the database remains consistent.
Information
π― Why Use ROLLBACK?
ROLLBACK protects data integrity by preventing incomplete or incorrect updates from being permanently stored.
- π Undo failed transactions.
- π Prevent partial database updates.
- π Recover safely from errors.
- π Maintain database consistency.
- π Support reliable business operations.
π‘ Real-World Example
Consider transferring $500 between two bank accounts:
- Withdraw $500 from Account A.
- Deposit $500 into Account B.
If the deposit fails because of an unexpected error, the withdrawal must also be canceled. Executing ROLLBACK restores both accounts to their original balances.
π Basic Syntax
ROLLBACK Syntax
BEGIN TRANSACTION;
-- SQL statements
ROLLBACK;β Example: Undo an UPDATE
Rollback an UPDATE
BEGIN TRANSACTION;
UPDATE Employees
SET Salary = Salary + 5000
WHERE EmployeeID = 101;
ROLLBACK;Since the transaction is rolled back, the employee's salary remains unchanged.
β Example: Roll Back Multiple Statements
A rollback reverses every uncommitted statement within the transaction.
Rollback Multiple Operations
BEGIN TRANSACTION;
INSERT INTO Orders
(OrderID, CustomerID, Amount)
VALUES
(1001, 25, 750);
UPDATE Inventory
SET Quantity = Quantity - 1
WHERE ProductID = 50;
ROLLBACK;Neither the new order nor the inventory update is saved because the entire transaction is canceled.
π Rolling Back to a Savepoint
Many database systems support SAVEPOINT, allowing you to undo only part of a transaction instead of canceling the entire transaction.
Rollback to a Savepoint
BEGIN TRANSACTION;
INSERT INTO Orders (...)
VALUES (...);
SAVEPOINT OrderCreated;
UPDATE Inventory
SET Quantity = Quantity - 1;
ROLLBACK TO SAVEPOINT OrderCreated;
COMMIT;Important
π Transaction Flow
| Step | Action |
|---|---|
| 1 | Begin the transaction. |
| 2 | Execute SQL statements. |
| 3 | An error or failure occurs. |
| 4 | Execute ROLLBACK. |
| 5 | The database returns to its previous state. |
βοΈ ROLLBACK vs COMMIT
| Feature | ROLLBACK | COMMIT |
|---|---|---|
| Purpose | Undo changes. | Save changes. |
| Database State | Restored. | Updated permanently. |
| Used After Failure | β Yes | β No |
| Used After Success | β No | β Yes |
π‘οΈ ROLLBACK and ACID
ROLLBACK helps maintain the ACID properties of database transactions.
| ACID Property | How ROLLBACK Contributes |
|---|---|
| Atomicity | Ensures all operations succeed together or none are applied. |
| Consistency | Restores the database to a valid state after failures. |
| Isolation | Prevents incomplete changes from becoming visible to other transactions. |
| Durability | Only committed data becomes permanent. |
Remember
πΌ Real-World Applications
- π¦ Bank transfers.
- π Online shopping checkouts.
- π¦ Inventory management.
- ποΈ Ticket reservation systems.
- π₯ Healthcare record updates.
- π³ Payment processing.
ποΈ Database Compatibility
| Database System | ROLLBACK Support |
|---|---|
| MySQL | β Supported by transactional storage engines such as InnoDB. |
| PostgreSQL | β Full transaction support. |
| SQL Server | β Full transaction support. |
| Oracle | β Full transaction support. |
| SQLite | β Full transaction support. |
β οΈ Common Mistakes
- β Expecting ROLLBACK to undo committed transactions.
- β Forgetting to handle errors that require a rollback.
- β Assuming every SQL statement is automatically part of the same explicit transaction.
- β Leaving transactions open after an error.
Warning
β οΈ Best Practices
Best Practice
π Key Points to Remember
- π ROLLBACK cancels all uncommitted changes in a transaction.
- π It restores the database to its previous consistent state.
- π It is commonly used after errors or unexpected failures.
- π Savepoints allow partial rollbacks in many database systems.
- π Committed changes cannot be undone with ROLLBACK.
- π ROLLBACK is essential for maintaining ACID transaction guarantees.