MySQL表共享读锁(Table Read Lock)和表独占写锁(Table Write Lock)

来源:互联网 发布:淘宝上的消字灵管用吗 编辑:程序博客网 时间:2024/05/20 07:18



       -- 表锁
-- 查看哪些表被锁
show open TABLES
-- 创建一个张表演示表锁,使用myisam存储引擎
CREATE table test_lock(
id int,
name VARCHAR(255)
)engine myisam;


-- 给表加锁读
lock table test_lock READ;
-- 会阻塞自己和其他用户的update操作
insert into test_lock(id,name)VALUES(2,'liyue');
update test_lock set name='l' where id=1;
SELECT * from test_lock;


-- 解锁
UNLOCK TABLES;


-- 给表加独占写锁
lock table test_lock WRITE;
-- 会阻塞其他用户的update操作,对自己update后没有释放锁的情况下
-- 只能在当前窗口查看不可打开一个新的窗口要不会视为一个新的session,由于锁被占用而无法查看
update test_lock set name='fi' where id=1;
SELECT * from test_lock;

0 0