MYSQL—加写锁,加读锁,解锁

链接地址:http://blog.sina.com.cn/s/blog_7fa2bcf50101j1lu.html

表级锁:

加写锁:

         lock   tables   table_name   read;//其他事务只能读,不能加写锁,要等待更新。

加读锁:

        lock   tables   table_name write;//其他事务不能读

解锁:

        unlock  tables;

---------------------------------------------

行级锁:行锁是对索引加锁

id    num

0 100

1 500

5 1000

例1.

事务1:set autocommit = off;

update test set num = 200 where id = 5; 

事务2:select * from test where id=5;可读,得到5 1000;

 

例2.

事务1:set autocommit = off;

update test set num = 200 where id = 5; 

事务2:set autocommit = off;

update test set num = 200 where id = 5;事务2等待事务1提交了才能更新。

例3.

事务1:set autocommit = off;

update test set num = 200 where id = 5; 

事务2:set autocommit = off;

update test set num = 200 where id = 6;事务2不用等待

例4.

事务1:set autocommit = off;

update test set num = 200 where id = 5; 

事务2:insert test (id) value (5);事务2等待,事务1提交后,事务2报错Duplicate entry '5' for key 'PRIMARY'

例5.

事务1:set autocommit = off;

update test set num = 200 where id = 5; 

事务2:insert test (id) value (6);事务2不用等待;

例6.

事务1:insert test (id) value (8); 

事务2:insert test (id) value (9);事务2不用等待;

 

由上可知为什么说行锁是对索引加锁。

如果一件事情你觉得难的完不成,你可以把它分为若干步,并不断寻找合适的方法。最后你发现你会是个超人。不要给自己找麻烦,但遇到麻烦绝不怕,更不要退缩。 电工查找电路不通点的最快方法是:分段诊断排除,快速定位。你有什么启示吗? 求知若饥,虚心若愚。 当你对一个事情掌控不足的时候,你需要做的就是“梳理”,并制定相应的规章制度,并使资源各司其职。
原文地址:https://www.cnblogs.com/wvqusrtg/p/5291022.html