sql statement to the database lock and unlock tables

Lock is a very important concept in the database, it is mainly used to ensure database integrity and consistency of the multi-user environment.  We know that multiple users can manipulate data in the same database at the same time, data inconsistencies can occur. If that is not locked and multiple users simultaneously access a database, then when their business while using the same data may be a problem. These include: lost updates, dirty reads, non-repeatable read and read illusion:

1. Lost update:

When two or more transactions select the same row, and then selected based on the initial value to update the row, lost update problem occurs. Each transaction is not aware of the existence of other transactions. The last update overwrites the update made by other firms, which would cause data loss. For example, two editors made electronic copies of the same document. Each editor independently change its replica, and then save a copy of the change, thus covering the original document. Finally, save the changes to a copy of the editorial staff covering the first change made by the editorial staff. If the second editor to make changes after the first editors completed, you can avoid this problem.

2. Dirty read

Dirty read refers to when a transaction is accessing data, and the data has been modified, and this modification has not been submitted to the database, then, another transaction also access the data, and then use this data. This data because the data is not yet committed, then another transaction read this data is dirty data, the operation may be made to the dirty data is incorrect. For example, an editor who is changing the electronic document. In the change process, other editors copied the document (which contains a copy of all the changes made so far) and distribute it to the intended user. Since then, the first editorial staff believe the changes currently is wrong, then I removed the edits and save the document. Distributed to the user's document contains edits that no longer exist, and these edits should believe never existed. If the first editorial staff to determine the final change before anyone can read the document changes, you can avoid this problem.

3. Non-repeatable read

Non-repeatable read means within a transaction, reading the same data multiple times. When this transaction is not over, another transaction also access the same data. So, between the two read data in the first transaction, due to the modification of the second transaction, then the first two transactions read data may be different. This occurs in a transaction the two read data are not the same, so called non-repeatable read. For example, an editor who read the same document twice, but between the two readings, the authors rewrite the document. When the second editorial staff read the document, the document has changed. Original reading can not be repeated. If only the author completed the preparation of the editorial staff can read the document, you can avoid this problem.

4. Read hallucinations

It refers to a read illusion phenomenon occurs when the transaction is not performed independently, for example, a first transaction data in the table has been modified, this modification involves all the data rows in the table. At the same time, also the second transaction to modify the data in this table, this modification is to insert a new row to the table. Then, the user operates the first transaction will occur after the discovery of the table there is no modification of the data lines, as if the same happened hallucinations. For example, a change document authors to submit editorial staff, but when the production department will change its content merged into the master copy of the document, the authors have found that the addition of new unedited material into the document. If before the editorial staff and production departments to complete the processing of the original document, no one can add new material to your document, you can avoid the problem 

 

SELECT statement described "locking option" feature

SQL Server provides a powerful and complete lock mechanism to help achieve concurrency and high-performance database system. Users can not only use the default settings SQL Server can also be used "in the select statement lock option effect" to achieve the expected

1. NOLOCK (unlocked)

When this option is selected, SQL Server without any locks when reading or modifying data. In this case, the user is likely to read data transaction is not completed (Uncommited Transaction) or rolled back (Roll Back), i.e. so-called "dirty."

2. HOLDLOCK (kept lock)

When this option is selected, SQL Server shared lock will remain until the end of this entire transaction, without releasing the way.

SELECT * FROM table WITH (HOLDLOCK)
Other transactions can read the table, but you can not delete update

3. UPDLOCK (modified lock)

When this option is selected, SQL Server uses modified lock when reading data instead of sharing the lock, and this lock is held until the end of the entire transaction or command. Use this option to ensure that multiple processes can simultaneously read the data but the process can only modify the data.

UPDLOCK.UPDLOCK advantage that allows you to read data (without blocking other transactions) and updated data later, while ensuring that data since the last read data has not been changed. When we use UPDLOCK to read can be recorded on the record to get together with update locks, so lock plus a record is to change only after the end of this thread and so can not be changed in the affairs of other thread.

Example:

在另一个查询里:
BEGIN TRANSACTION
SELECT * FROM myTable WITH (UPDLOCK) WHERE Id in (1,2,3)
waitfor delay '00:00:10' 
update myTable set [Name]='ZZ' where Id in (1,2,3)
commit TRANSACTION

In another query in:
SELECT * FROM myTable WHERE Id in (1,2,3)
You can query the data immediately.

But if you want to update the data, such as the need to perform other update locks are released.
update myTable set [Name]='ZZ' where Id in (1,2,3)

This shows that sometimes need to control a record not allowed to be updated after I read, so I can deal with all queries to the current record are coupled with update locks to prevent other transactions from modifying queries after. The impact of the transaction is reduced to a minimum

4. TABLOCK (lock table)

When this option is selected, SQL Server will set shared locks until the end of the command on the entire table. This option ensures that other processes can read but can not modify the data.

5. TABLOCKX (exclusive table lock)

When this option is selected, SQL Server will set an exclusive lock on the entire table until the end of the order or transaction. This will prevent other processes to read or modify data in the table.

SELECT * FROM table WITH (TABLOCKX)

其他事务不能读取表,更新和删除

6. PAGLOCK(页锁)

此选项为默认选项, 当被选中时,SQL Server 使用共享页锁。

7. ROWLOCK (强制使用行锁) 

一直有个疑问,使用 select * from dbo.A with(RowLock) WHRE a=1 这样的语句,系统是什么时候释放行锁呢??
经过官方文档考证后,原来 RowLock在不使用组合的情况下是没有任何意义的,所谓“解铃还须系铃人~”
With(RowLock,UpdLock) 这样的组合才成立,查询出来的数据使用RowLock来锁定,当数据被Update的时候,或者回滚之后,锁将被释放 

 

Guess you like

Origin www.cnblogs.com/zhaoyl9/p/10856170.html
Recommended