Mark As Completed Discussion

Rollback Example

All changes have to be stopped immediately if an error with any of the SQL-grouped statements occurs. Rollback refers to this action of undoing changes in such scenarios. Any transactions that haven't been recorded to the database yet will be rolled back using the ROLLBACK command, allowing us to start from the beginning of the transaction. Only actions taken after the last COMMIT or ROLLBACK command can be reversed with this command.

For example, let’s write a transaction for deleting all orders where the customer’s age is over 40, and then undo the changes, like so:

TEXT/X-SQL
1-- Start the new transaction    
2BEGIN TRANSACTION  
3-- SQL Statements  
4 DELETE FROM Table2 WHERE Age > 40
5 -- undo changes   
6ROLLBACK TRANSACTION  

After the execution of the transaction, the resulting table would look exactly the same as before, because we rolled back the changes.