SQL ROLLBACK: How to Undo Transactions
In SQL, the ROLLBACK statement can be used to roll back a transaction. When a transaction encounters an issue or needs to undo a previous operation, the ROLLBACK statement can be used to revert the transaction to its previous state. After executing the ROLLBACK statement, all SQL operations that have been performed will be undone, and the database will return to its state before the transaction began. An example is provided below:
START TRANSACTION; -- 开始事务
UPDATE table_name SET column1 = value1 WHERE condition; -- 执行SQL操作
INSERT INTO table_name (column1, column2) VALUES (value1, value2); -- 执行SQL操作
-- 如果需要回滚事务,可以使用ROLLBACK语句
ROLLBACK; -- 回滚事务