Home >Database >Mysql Tutorial >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!