Skip to content

ROLLBACK

一句话解释

ROLLBACK 是 SQL command 那个取消 current 事务 and undoes 所有 uncommitted 更改.

用大白话说

ROLLBACK ends a transaction by undoing all its changes. The database returns to the state it was in before the transaction began. This is how you recover from errors — if something goes wrong mid-transaction, you roll back and no partial changes are saved.

A common pattern is to try a series of operations and roll back if any one fails:

BEGIN;
-- 操作 1
-- 操作 2 (失败!)
ROLLBACK; -- 操作 1 是 也 undone

ROLLBACK satisfies the Atomicity property of ACID — the transaction is all-or-nothing, and rollback ensures "无" when it cannot be "所有."

示例

sql
BEGIN;
UPDATE accounts SET balance = balance - 500 WHERE id = 1;
-- Oops, account 2 does not exist!
UPDATE accounts SET balance = balance + 500 WHERE id = 999; -- 0 rows affected
ROLLBACK; -- The first UPDATE is undone

ROLLBACK undoes 首先 UPDATE, 所以 account 1's balance 是 restored.

相关术语

相关资源

相关课程

  • 事务

    学习 COMMIT and ROLLBACK.

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表