命令行窗口1 | 命令行窗口2 | 命令行窗口3 |
---|
mysql> set autocommit = 0; Query OK, 0 rows affected mysql> select * from developerinfo where userid = '1' for update; +--------+--------+----------+ | userID | name | passWord | +--------+--------+----------+ | 1 | liujie | 123456 | +--------+--------+----------+ 1 row in set |mysql> set autocommit = 0; Query OK, 0 rows affected mysql> select * from developerinfo where userid = '1' for update; 等待 |mysql> set autocommit = 0; Query OK, 0 rows affected mysql> select * from developerinfo where userid = '3' for update; +--------+------+----------+ | userID | name | passWord | +--------+------+----------+ | 3 | tong | 123456 | +--------+------+----------+ 1 row in set |mysql> commit; Query OK, 0 rows affected mysql> select * from developerinfo where userid = '1' for update; +--------+--------+----------+ | userID | name | passWord | +--------+--------+----------+ | 1 | liujie | 123456 | +--------+--------+----------+ 1 row in set
(2)查询非索引的字段来查询数据库使用行锁
打开两个命令行窗口进行测试
命令行窗口1 | 命令行窗口2 |
---|
|mysql> set autocommit=0; Query OK, 0 rows affected mysql> select * from developerinfo where name = 'liujie' for update; +--------+--------+----------+ userID name passWord +--------+--------+----------+ 1 liujie 123456 +--------+--------+----------+ 1 row in set |mysql> set autocommit=0; Query OK, 0 rows affected mysql> select * from developerinfo where name = 'tong' for update; 等待| mysql> commit; Query OK, 0 rows affected mysql> select * from developerinfo where name = 'liujie' for update; +--------+--------+----------+ | userID | name | passWord | +--------+--------+----------+ | 1 | liujie | 123456 | +--------+--------+----------+ 1 row in set
##### (3)查询非唯一索引字段来查询数据库使用行锁锁住多行
mysql的行锁是针对索引假的锁,不是针对记录,所以可能会出现锁住不同记录的场景
打开三个命令行窗口进行测试
命令行窗口1 命令行窗口2 命令行窗口3
mysql> set autocommit=0; Query OK, 0 rows affected mysql> select * from developerinfo where password = '123456 ' for update; +--------+--------+----------+ | userID | name | passWord | +--------+--------+----------+ | 1 | liujie | 123456 | | 3 | tong | 123456 | +--------+--------+----------+ 2 rows in set mysql> set autocommit =0 ; Query OK, 0 rows affected mysql> select * from developerinfo where userid = '1' for update;
等待
mysql> set autocommit = 0; Query OK, 0 rows affected mysql> select * from developerinfo where userid = '2 ' for update; +--------+--------+----------+ | userID | name | passWord | +--------+--------+----------+ | 2 | yitong | 123 | +--------+--------+----------+ 1 row in set commit; mysql> select * from developerinfo where userid = '1' for update; +--------+--------+----------+ | userID | name | passWord | +--------+--------+----------+ | 1 | liujie | 123456 | +--------+--------+----------+ 1 row in set
##### (4)条件中使用索引来操作检索数据库时,是否使用索引还需有mysql通过判断不同执行计划来决定,是否使用该索引,如需判定如何使用explain来判断索引,请听下回分解
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。
标签:鄂尔多斯 黔西 昌都 驻马店 荆门 梅河口 陕西 北京
巨人网络通讯声明:本文标题《MySQL中的行级锁定示例详解》,本文关键词 MySQL,中的,行级,锁定,示例,;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。