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 :
start transaction; select userid,password,age from accountsdb.accounts_info where accounts = 'hello2' for update;commit;
相关学习推荐:mysql数据库
以上就是mysql怎么加行锁?的详细内容。
