Firstly, we have to understand what is lock and where we put locks in SQL Server and then different types of locks.
What is Lock in SQL Server?
As we all know, multiple users need to access databases concurrently. So locks come into the picture to prevent data from being corrupted or invalidated when multiple users try to do operations such as read, write and update on database.
“Lock is defined as a mechanism to ensure data integrity, consistency while allowing concurrent access to data. It is used to implement concurrency control when multiple users access Database to manipulate its data at the same time”
Where locks are put in Database
Now, we have to understand where locks are actually present in our database, it means on which resource it locks or not.
RID: (Row ID)
RID Used to lock a single row within a table.
Table: Complete table, including all data and indexes.
Key: Row lock within an index. It means primary key, Candidate Key, Secondary key etc.
Page: 8-kilobyte (KB) data page or index page. Lock can be placed on Page Level also, it means if a particular page is locked so another user cannot update data on it.
Extent: Contiguous group of eight data pages which can include index pages also.
Database: Entire Database can be locked for some type of users who have read permission on database.
Different Models of SQL Server locks
- Shared(S)
- Used for select operations
- Enable other sessions to perform select operations but prevent updates
- read-only operations
- Operation with SELECT statement generally use in Shared mode.
- Exclusive(X)
- Used for DML operations
- Prevents other users from accessing the resource.
- Operations, such as INSERT, UPDATE, or DELETE means DML query. Ensures that multiple updates cannot be made to the same resource at the same time.
- Update(U)
- Preliminary stage for exclusive lock. Used by the server when filtering the records to be modified
- Prevents other update locks
- A solution to the cycle deadlock problem
- Intent
- Intent Locks are used for establishing a lock Hierarchy.
- The types of intent locks are:
- intent shared (IS),
- intent exclusive (IX)
- shared with intent exclusive (SIX).
- Schema
- Schema locks are used when an operation dependent on the schema of a table is executing.
- The types of schema locks are:
- Schema modification (Sch-M) and
- Schema stability (Sch-S).
- Bulk Update (BU)
- Bulk Update used when bulk-copying data into a table and the TABLOCK hint is specified. Generally, use when user want to insert huge data in database/
Examples of Locks in SQL Server
Shared lock: select balance from tbl_account where acct_number = 25
--shared lock
We can perform multiple select statements on the same table.
We can perform multiple select statements on the same table.
Exclusive lock
insert tbl_account values(34, 500)
When we perform insert query in the table then page lock in Exclusive mode. Until recorded it's not inserted in table n other operation perform here. Similarly, delete, Update operation occurs.
delete tbl_account where balance < 0
update tbl_account set balance = 0 where acct_number = 25
Lock Compatibility Matrix
Read more articles on SQL Server:


mohammad siddiquiPosted Nov 18, 2020, 10:40 PM EST
My Question: If A user is working and other B user want to work on same table, B user can get message that data is hold by A user.
harish kumarPosted Aug 19, 2020, 12:53 AM EDT
Great Article...
Jaiprakash BarnwalPosted Aug 8, 2019, 7:40 AM EDT
"Different Models of SQL Server locks", I think it should be "Different Modes of SQL Server locks"
yesubabu kPosted Jan 4, 2019, 9:23 PM EST
It is very helpfull
Yashwant VishwakarmaPosted Feb 21, 2017, 7:07 PM EST
Nice article :) Keep on writing !!
Delpin Susai RajTOP 100Posted Aug 28, 2016, 1:02 AM EDT
Nice
SubashPosted Jul 3, 2016, 9:05 PM EDT
Thanks
Mithilesh KumarPosted Jun 7, 2016, 4:34 AM EDT
Very Nice
Kapi ShivharePosted May 16, 2016, 8:23 PM EDT
Nice...
Jessica JazzPosted May 16, 2016, 8:23 PM EDT
Nice explanation..
Saravanakumar SekaranPosted May 14, 2016, 6:21 PM EDT
wow really nice.
Prasanna MuraliPosted May 12, 2016, 6:02 PM EDT
nice
Vishal PrajapatiPosted May 2, 2016, 3:00 AM EDT
Good Job...
NitinPosted May 1, 2016, 8:20 AM EDT
nice
Kuppurasu NagarajTOP 100Posted Apr 30, 2016, 10:56 AM EDT
Nice Sharing..
Pankaj Kumar ChoudharyTOP 100Posted Apr 30, 2016, 9:14 AM EDT
Nice Explain.......
Vivek KumarPosted Apr 28, 2016, 10:42 AM EDT
Good one
RakeshPosted Apr 28, 2016, 9:58 AM EDT
Good one
Humayun Kabir MamunPosted Apr 28, 2016, 9:51 AM EDT
Nice...
Mohammed IbrahimPosted Apr 28, 2016, 8:21 AM EDT
nice
Thiruppathi RPosted Apr 28, 2016, 5:07 AM EDT
Great info
Hari ShankerPosted Apr 27, 2016, 7:34 PM EDT
Nice information
sreenivasa kPosted Apr 27, 2016, 7:30 PM EDT
good share
Bhavik PatelPosted Apr 27, 2016, 5:17 PM EDT
good share. useful