Home >Database >Mysql Tutorial >How to add row lock in mysql?

How to add row lock in mysql?

藏色散人
藏色散人Original
2019-05-20 13:14:1016583browse

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; #提交

Query 2:

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

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!

Statement:
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
Previous article:What is mysql notifier?Next article:What is mysql notifier?