仅在部分行上防止幻读
您需要分析银行客户中 customer_id 在 1 到 10 之间的一些数据。您只想锁定 customers 表中 customer_id 在 1 到 10 之间的那些行。这样可以确保没有人能够修改这些行,同时也允许其他事务处理表中的其余数据。
您需要再次选择这些客户并执行一些数学运算。(本练习不关注这些运算本身。)之后,您想再次选择 customer_id 在 1 到 10 之间的客户,以确保没有任何变化。
该如何编写脚本?
本练习是课程的一部分
SQL Server 中的事务与错误处理
练习说明
- 设置合适的隔离级别以防止幻读。
- 开启一个事务。
- 选择您要锁定的那些客户。
- 提交事务。
交互式实操练习
通过完成这段示例代码来试试这个练习。
-- Set the appropriate isolation level
___ ___ ___ ___ ___
-- Begin a transaction
___ ___
-- Select customer_id between 1 and 10
SELECT *
FROM customers
___ customer_id ___ ___ AND ___;
-- After completing some mathematical operation, select customer_id between 1 and 10
SELECT *
FROM customers
___ customer_id ___ ___ AND ___;
-- Commit the transaction
___ ___