事务
一句话解释
事务是 序列的 数据库操作那个是 treated as one 单元 — 要么所有 succeed or 所有是 rolled 后面.
用大白话说
A transaction is a group of database operations that are executed as a single unit of work. The key property is "所有 or 无" — if any operation in the transaction fails, the entire transaction is rolled back and the database is unchanged. If all operations succeed, the transaction is committed and the changes become permanent.
Transactions are essential when multiple operations must happen together. For example, a bank transfer: deduct from account A and add to account B. If the second operation fails, the first must be undone — otherwise money disappears.
In SQL, you start a transaction with BEGIN, make changes, then either COMMIT (save) or ROLLBACK (undo). Transactions follow the ACID properties: Atomicity, Consistency, Isolation, Durability.
示例
BEGIN;
-- Transfer $100 from account 1 to account 2
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
-- If both succeed, save the changes
COMMIT;
-- If anything fails: ROLLBACK;两者都更新是 in one 事务 — 要么两者都 happen or neither 做.
相关术语
相关资源
相关课程
- 事务
学习如何事务工作.
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。