SQLite ROLLBACK Statement
SQLite ROLLBACK Statement
Let’s learn about the SQLite ROLLBACK statement in this tutorial. This statement is used to undo the changes made by the transaction in case of failure.
The ROLLBACK statement aborts any changes made in the transaction and does not save any changes to the database.
Syntax
The general syntax of the statement is as follows:
ROLLBACK;
There are no additional clauses or options to the statement. The ROLLBACK statement can only be used to undo uncommitted transactions since the last COMMIT or ROLLBACK statement.
Example
This example demonstrates the usage of the ROLLBACK statement. The transaction
attempts to update two records of the emp table as part of the transaction.
BEGIN TRANSACTION;
UPDATE emp SET salary = 8000 WHERE empno = 1;
UPDATE emp SET salary = 8500 WHERE empno = 6;
SELECT * FROM emp;
ROLLBACK;
SELECT * FROM emp;
A SELECT on the emp executed as part of the transaction shows updated data to confirm the operations. When the ROLLBACK command is issued, the changes made by the UPDATE statements are undone. After the ROLLBACK, the SELECT shows the original data before the transaction.
If a transaction is not explicitly terminated and encounters a hardware or software failure, it is rolled back automatically.
SQLite Tutorials
SQLite tutorials on this website: