ROLLBACK: Undo changes made in a transaction in sql

3/5/2024

Undo Changes in a Transaction # ROLLBACK: Undo changes made in a transaction in sql

Go Back

ROLLBACK in SQL: How to Undo Changes in a Transaction

In SQL, the ROLLBACK statement is used to undo changes made during a transaction that has not been permanently saved to the database. It is a crucial feature for maintaining data integrity and managing errors in database operations. This article will explain how to use ROLLBACK in SQL, along with examples and best practices.

Undo Changes in a Transaction # ROLLBACK: Undo changes made in a transaction in sql

What is ROLLBACK in SQL?

The ROLLBACK statement reverses any changes made during the current transaction and restores the database to its state before the transaction began. It is typically used in conjunction with the BEGIN TRANSACTION and COMMIT statements to manage database transactions effectively.

Syntax of ROLLBACK

The basic syntax of the ROLLBACK statement is:


ROLLBACK;
    

This command undoes all changes made in the current transaction.

Using ROLLBACK with BEGIN TRANSACTION and COMMIT

Here’s an example of how to use ROLLBACK with BEGIN TRANSACTION and COMMIT:


BEGIN TRANSACTION;

-- Perform some database operations (e.g., INSERT, UPDATE, DELETE)
INSERT INTO employees (name, salary) VALUES ('John Doe', 50000);
UPDATE employees SET salary = 60000 WHERE name = 'Jane Doe';

-- If something goes wrong, rollback the transaction
ROLLBACK;

-- If everything is successful, commit the transaction
-- COMMIT;
    

In this example, if an error occurs or you decide to cancel the transaction, the ROLLBACK statement will undo all changes made during the transaction.

Key Points About ROLLBACK

  • Scope: ROLLBACK only affects the current transaction. It cannot undo changes made by previously committed transactions.
  • Use Case: ROLLBACK is commonly used in error handling to ensure data consistency.
  • Transaction Control: Always use BEGIN TRANSACTION to start a transaction and COMMIT to save changes permanently.

Example: ROLLBACK in Action

Let’s consider a scenario where you want to insert data into a table but rollback the transaction if an error occurs:


BEGIN TRANSACTION;

-- Insert data into the employees table
INSERT INTO employees (name, salary) VALUES ('Alice', 70000);

-- Simulate an error (e.g., division by zero)
DECLARE @result INT;
SET @result = 10 / 0;

-- If an error occurs, rollback the transaction
IF @@ERROR <> 0
BEGIN
    ROLLBACK;
    PRINT 'Transaction rolled back due to an error.';
END
ELSE
BEGIN
    COMMIT;
    PRINT 'Transaction committed successfully.';
END
    

In this example, the transaction is rolled back if an error occurs during execution.

Conclusion

The ROLLBACK statement is a powerful tool in SQL for undoing changes made during a transaction. By using it with BEGIN TRANSACTION and COMMIT, you can ensure data integrity and handle errors effectively. Whether you're working on a small project or a large-scale database, understanding ROLLBACK is essential for managing transactions in SQL.

Table of content