use*_*544 5 sql-server temporal
我有一个 SQL Server 时态表“person”,其记录如下:
name | modified_by
-------------------------
John | 1
Run Code Online (Sandbox Code Playgroud)
如果我执行以下sql语句
update person set modified_by = 2 where name = 'John'
delete from person where name = 'John'
Run Code Online (Sandbox Code Playgroud)
我在历史表中看到两条记录,分别为modified_by = 1和2。
如果我在一个事务中执行这两条语句
begin transaction
update person set modified_by = 2 where name = 'John'
delete from person where name = 'John'
commit transaction
Run Code Online (Sandbox Code Playgroud)
我在历史表中只看到一条带有modified_by = 1的记录
这是预期的行为吗?为什么?
小智 -1
是的,这是预期的。
事务中的所有语句要么完全执行,要么根本不执行。没有中间步骤。
在您的示例中,更新语句浪费资源,因为相关行无论如何都会被删除。
然而,如果没有事务,您的第一个语句将导致表的三个一致状态:第一个语句之前、更新之后和删除之后。