MySQL InnoDB lock table with lock line

Source: Internet
Author: User

Because the InnoDB preset is Row-level lock, MySQL executes row lock (only the selected data sample) only if the specified primary key is "clear", otherwise MySQL will execute table lock (lock 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)

Copy the Code code as follows:

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)

Copy the Code code as follows:

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

Example 3: (No primary key, table lock)

Copy the Code code as follows:

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

Example 4: (primary key ambiguous, table lock)

Copy the Code code as follows:

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

Example 5: (primary key ambiguous, table lock)

Copy the Code code as follows:

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.


MySQL InnoDB lock table with lock line

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.