SQL Server › Isolation & Locking
What is a lock in SQL Server, and why does the database engine use locks?
BeginnerAnswer
A lock is a mechanism SQL Server uses to control concurrent access to data, temporarily preventing other transactions from making conflicting changes to the same data at the same time, which protects the consistency and accuracy of your data when multiple users or processes are working with the database simultaneously.
BEGIN TRANSACTION;
UPDATE Accounts SET Balance = Balance - 100 WHERE AccountId = 1;
-- This row is now locked until the transaction commits or rolls back
COMMIT TRANSACTION;Real-world example A banking system locks an account row briefly during a balance update, preventing another transaction from reading or changing that same account's balance until the first update safely completes.