SQL BEGIN, COMMIT, ROLLBACK
Group changes so they all happen, or none do.
Syntax
BEGIN; …statements… COMMIT; -- or ROLLBACK;
ROLLBACK undoes everything since BEGIN.
Example
Query (on the shop database used in the SQL course)
BEGIN; DELETE FROM order_items; ROLLBACK; SELECT COUNT(*) AS items_still_there FROM order_items;
Result
| items_still_there |
|---|
| 116 |
1 row
Related
Learn it step by step in the free lesson Changing Data, with exercises that check your queries.