创建行锁条件:
1、表中创建索引, select 。。。 where 字段(必须是索引) 不然行锁就无效。
2、必须要有事务,这样才是 行锁(排他锁)
3、在select 语句后面 加 上 FOR UPDATE;
例子: 测试是否 加了锁,先执行 1 后执行 2
查询1
START TRANSACTION; #加事务
SELECT UserID,Password,Age FROM AccountsDB.Accounts_InFo WHERE Accounts = 'hello2' FOR UPDATE; #加锁
select SLEEP(5); # 睡眠
COMMIT; #提交
查询2 :
SELECT UserID,Password,Age FROM AccountsDB.Accounts_InFo WHERE Accounts = 'hello2' FOR UPDATE;
COMMIT;