mysql 表锁演示

来源:互联网 发布:照片叠加软件 编辑:程序博客网 时间:2024/06/16 14:06

详细页面:http://www.verydemo.com/demo_c152_i14639.html

首先看表结构,

+----+----------+------+
| id | name     | seat |
+----+----------+------+
|  1 | 管理员        |   98 |
|  2 | 维护人员       |   98 |
|  3 | 主任         |   97 |
|  4 | 班主任         |   96 |
+----+----------+------+



先看表锁的情况
发sql:

set autocommit=0;
select * from role where seat=98 for update;




再发一条sql查询:select * from role where id = 1;
显示结果:
+----+--------+------+
| id | name   | seat |
+----+--------+------+
|  1 | 管理员      |   98 |
+----+--------+------+
1 row in set (0.00 sec)

这是因为select 操作 无关乎锁定。

然后看写操作:update role set seat=99 where id =3;

可以看到,漫长的等待后(超过mysql默认的执行时间之后),显示如下结果


此时我们看锁定记录:

发sql:SHOW PROCESSLIST;

+----+------+-----------------+---------+---------+------+-------+--------------
----+
| Id | User | Host            | db      | Command | Time | State | Info
    |
+--…………………………………………………………………………
0 0
原创粉丝点击