Home > Database > Mysql Tutorial > body text

How to add row lock in mysql?

藏色散人
Release: 2020-09-19 14:23:14
Original
16562 people have browsed it

How to add row lock in mysql?

Create row lock conditions:

1. Create an index in the table, select ... where field (required is the index) otherwise the row lock will be invalid.

2. There must be a transaction, so that it is a row lock (exclusive lock)

3. Add FOR UPDATE after the select statement;

Example: Test whether to add If the lock is locked, execute 1 first and then 2

Query 1

START TRANSACTION;  #加事务
SELECT UserID,Password,Age FROM AccountsDB.Accounts_InFo    WHERE Accounts = 'hello2' FOR UPDATE;  #加锁
select SLEEP(5);  # 睡眠
COMMIT; #提交
Copy after login

Query 2:

START TRANSACTION;  
SELECT UserID,Password,Age FROM AccountsDB.Accounts_InFo    WHERE Accounts = 'hello2' FOR UPDATE;
COMMIT;
Copy after login

Related learning recommendations:mysql database

The above is the detailed content of How to add row lock in mysql?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template