防止資料表中的幻讀
今天你要分析銀行中每位客戶的資料。由於這些資訊非常重要,你考慮鎖定整個 customers 資料表,讓任何人都無法在這張表上做出變更。這樣做可以防止發生幻讀。
你會先撰寫一段指令來選取所需資訊,並根據選取結果進行一些數學運算(本練習不著重於這些運算)。之後,你想再次選取相同的資料,確認期間沒有任何變動。
本練習屬於課程
SQL Server 的交易與錯誤處理
練習說明
- 設定合適的隔離層級以防止幻讀。
- 開始交易。
- 提交交易。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
-- Set the appropriate isolation level
___ ___ ___ ___ ___
-- Begin a transaction
___ ___
SELECT * FROM customers;
-- After some mathematical operations, we selected information from the customers table.
SELECT * FROM customers;
-- Commit the transaction
___ ___