Careeroza
ROLLBACK
The ROLLBACK Statement
ROLLBACK undoes every change made since the transaction began. Use it when something goes wrong halfway.
Example
Delete all orders, change your mind, roll back. The orders are still there:
BEGIN TRANSACTION;
DELETE FROM Orders;
ROLLBACK;
SELECT COUNT(*) AS OrdersStillThere FROM Orders;Result: 1 row
| OrdersStillThere |
|---|
| 8 |
Automatic Rollback
If the connection drops or the server crashes before COMMIT, the database rolls the transaction back automatically when it restarts. A half-finished transfer can never be left behind.
BEGIN;
UPDATE Accounts SET Balance = Balance - 500 WHERE AccountID = 1;
UPDATE Accounts SET Balance = Balance + 500 WHERE AccountID = 2;
-- if either statement fails, the application runs ROLLBACK
COMMIT;