Database record lock table lock practical research note --- MSSQLSERVER

Source: Internet
Author: User
Directly cut the topic. There is A table in the table: ChenJiID, DanWeiID, Name, and ChenJi. The IDDanWeiIDNameChenJi ---------------------------- the consumer opens two connections in the query analyzer to connect A to begintrant.

There is a table in the direct cut topic: ChenJi ID, DanWeiID, Name, in the ChenJi table, record ID DanWeiID Name ChenJi --- ----------- --------- 1 1 aa 91 2 1 bb 91 3 1 cc 33 4 2 dd 164 5 2 ee 155 6 2 ff 166 in the query analyzer. two connections A and begin tran t

Straight cut topic

An existing table

Table: ChenJi

ID, DanWeiID, Name, ChenJi

Table records

ID DanWeiID Name ChenJi
--------------------------------
1 1 aa 91
2 1 bb 91
3 1 cc 33
4 2 dd 164
5 2 ee 155
6 2 ff 166

Open two connections in the query Analyzer

A connection

Begin tran t1

Insert into ChenJi (DanWeiID, Name, ChenJi) values (3, 'zhang san', 98)

Rollback tran t1

B connection

Insert and lock

Start the transaction connected to A first, and end the transaction in no hurry

Then execute the following statements in sequence in connection B:

1. insert into ChenJi (DanWeiID, Name, ChenJi) values (3, 'lily', 99) can execute the insert statement

2. select * from ChenJi needs to wait

3. select * from ChenJi where Name = 'A' need to wait

3. update ChenJi SET ChenJi = 91 WHERE Name = 'A' to wait

4. delete from ChenJi where Name = 'A' need to wait

The study found that if a transaction execution insert statement is enabled in a table, the select, update, and delete statements of the table will be rejected and locked. However, other connections can also be used to insert the table.

Update and lock

Open two connections in the query Analyzer

A connection

Begin tran t1

Update ChenJi SET ChenJi = 91 WHERE Name = 'A'

Rollback tran t1

Start the transaction connected to A first, and end the transaction in no hurry

Then execute the following statements in sequence in connection B:

1. update ChenJi SET ChenJi = 91 WHERE Name = 'A' to wait

2. select * from ChenJi where Name = 'A' need to wait

3. select * from ChenJi where Name = 'bb' needs to wait

4. select * from ChenJi where ChenJi = 0 waiting

5. update ChenJi SET ChenJi = 91 WHERE Name = 'bb'. Please wait.

6. delete from ChenJi where Name = 'bb' needs to wait

7. insert into ChenJi (ID, DanWeiID, Name, ChenJi) values (100, 3, 'ymf ', 98) can execute the insert statement

Studies have found that if an update statement is enabled for a transaction in a table, the select, update, and delete statements of the table will be rejected and locked. However, other connections can also be used to insert the table.

Primary Key and lock

Open two connections in the query Analyzer

Add the primary key ID int type to the ChenJi table

A connection

Begin tran t1

Update ChenJi SET ChenJi = 91 where id = 1

Rollback tran t1

Start the transaction connected to A first, and end the transaction in no hurry

Then execute the following statements in sequence in connection B:

1. update ChenJi SET ChenJi = 91 WHERE Name = 'A' to wait

2. select * from ChenJi where Name = 'A' need to wait

3. select * from ChenJi where ID = 1 need to wait -- because name = 'A'

4. select * from ChenJi where ChenJi = 0 waiting

5. update ChenJi SET ChenJi = 91 WHERE Name = 'bb' can be executed

6. delete from ChenJi where Name = 'bb' can be executed

7. insert into ChenJi (ID, DanWeiID, Name, ChenJi) values (100, 3, 'ymf ', 98) can execute the statement

8. update ChenJi SET ChenJi = 91 where id = 2 can be executed

9. select * from ChenJi where ID = 2 can be executed

10. delete from ChenJi where ID = 2 can be executed

The study found that if a transaction execution update statement is enabled in a table and the primary key is used as the condition, the select, update, and delete records corresponding to the primary key of the table are rejected and locked, yes. The table can be inserted in other connections, and other records can be operated normally.

Index lock

Open two connections in the query Analyzer

Index the Name field on the ChenJi table <不是唯一性的索引> , ID is still the primary key

A connection

Begin tran t1

Update ChenJi SET ChenJi = 91 WHERE name = 'A'

Rollback tran t1

Start the transaction connected to A first, and end the transaction in no hurry

Then execute the following statements in sequence in connection B:

1. update ChenJi SET ChenJi = 91 WHERE Name = 'A' to wait

2. select * from ChenJi where Name = 'A' need to wait

3. select * from ChenJi where Name = 'bb' can be executed

4. select * from ChenJi where ChenJi = 0 waiting

5. update ChenJi SET ChenJi = 91 WHERE Name = 'bb' can be executed

6. delete from ChenJi where Name = 'bb' can be executed

7. insert into ChenJi (ID, DanWeiID, Name, ChenJi) values (100, 3, 'ymf ', 98) can execute the statement

8. update ChenJi SET ChenJi = 91 where id = 2 can be executed

9. select * from ChenJi where ID = 2 can be executed

10. delete from ChenJi where ID = 2 can be executed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.