Problems with the SELECT * For Update lock table in MySQL (GO)

Source: Internet
Author: User

Since the InnoDB preset is row-level lock, MySQL will execute row lock only if the specified primary key is "clear" (only the selected data is locked), otherwise MySQL executes the table

Lock (locks the entire data form). For example: Suppose there is a form products with ID and name two fields, ID is the primary key.

Example 1: (explicitly specify the primary key, and there is this information, row lock)

SELECT * FROM Products WHERE id= ' 3 ' for UPDATE;

SELECT * FROM Products WHERE id= ' 3 ' and type=1 for UPDATE;

Example 2: (explicitly specify the primary key, if the information is not found, no lock)

SELECT * FROM Products WHERE id= '-1 ' for UPDATE;

Example 2: (No primary key, table lock)

SELECT * FROM Products WHERE name= ' Mouse ' for UPDATE;

Example 3: (primary key ambiguous, table lock)

SELECT * FROM Products WHERE id<> ' 3 ' for UPDATE;

Example 4: (primary key ambiguous, table lock)

SELECT * from the products WHERE ID like ' 3 ' for UPDATE;
Note 1:for Update applies only to InnoDB and must be in the transaction block (Begin/commit) to take effect.

NOTE 2: To test the condition of the lock, you can use the command Mode of MySQL and open two windows to do the test.
Testing in MySQL 5.0 is exactly like this.

In addition: Myasim only support table-level lock, INNERDB support row-level lock added (row-level lock/table-level Lock) lock data can not be locked by other transactions, and not be modified by other transactions

(Modify, delete). is a table-level lock, the table is locked regardless of whether the record is queried or not .

Http://www.cnblogs.com/xiohao/p/4385768.html

Problems with the SELECT * For Update lock table in MySQL (GO)

Related Article

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.