Answers:
要增加DylanSp的答案,尝试更新不存在的记录中的字段将失败,但是结果仍将是预期的结果:记录r不存在。
但是,请考虑删除记录实际上将失败的情况:
让我们(并非不现实地)假设每个OrderLine 必须与一个Order相关。
现在,从删除订单开始回滚事务将失败,因为这将违反我们的业务规则。
所以之后
我们可能会以现有订单(没有订单行)结束。
当然,在这种情况下,我们可以使用级联删除,但这将意味着步骤2将失败(无影响)。更糟糕的是,对订单实施级联删除可能是不受欢迎的行为。
假设有一个仅包含一列的表T。
假定“撤消日志”是一个包含未提交事务的数据库文件,并且“重做日志”是一个包含尚未应用到数据文件的未提交事务和已提交事务的数据库文件。
At 8:00 A.M., Transaction 100 inserts rows with values 101, 102 and 103
into table T.
At 8:10 A.M., Transaction 100 is committed and the commit for
transaction 100 completes.
At 8:15 A.M., Transaction 200 updates row 101 to 201, 102 to 202
and 103 to 203.
At 8:20 A.M., Transaction 200 has not been committed and remains
in the undo log of the database.
At 8:25 A.M., Transaction 300 increments each row by 50,
changing row 201 to 251, 202 to 252, and 203 to 253.
At 8:30 A.M., Transaction 300 has not been committed and remains
in the undo log of the database.
At 8:35 A.M., The instance providing access to the database crashes.
At 8:40 A.M., The instance is restarted, and the database files are
opened as the instance is started:
The committed values in T are still 101, 102 and 103.
Since 201, 202, and 203, and 251, 252 and 253
are not committed, if they are written into the "redo
log" of the database, there is a need to "roll back"
the transactions AFTER the "redo log" is applied.
Since 201, 202, and 203, and 251, 252 and 253
are not committed, they are in the "undo log"
of the database.
The undo log of the database is used BOTH to (1) roll
back a transaction that is deliberately rolled
back in the memory structure of the database instance,
and also (2) during the instance recovery at 8:40 A.M.
At 8:41 A.M., The redo log has been applied, and the T table
contains values 251, 252 and 253 in the instance memory.
The undo log has not yet been applied.
At 8:42 A.M., The undo log is applied in the reverse order:
Uncommitted transaction 300 is undone, and
Uncommitted transaction 200 is undone.
为什么将已提交和未提交的事务都写入重做日志文件? 这样做的原因是提供时间点恢复。
这意味着“重做日志”文件的内容与事务不一致。 因此,无论何时使用重做日志将已提交的事务应用于数据文件,都必须使用“撤消日志”来回滚未提交的事务。
为什么“撤消日志”中的事务以相反的顺序回滚?事务300已经将50增加到每一行的每一列的现有值。因此,如果事务200首先回滚,则值将从251、252和253更改为201、202和203。如果事务300然后最后回滚,则值将为151、152和153-不匹配原始承诺值。
参考资料:
https://asktom.oracle.com/pls/asktom/f?p=100:11:0:::::P11_QUESTION_ID:1670195800346464273