链接地址: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不用等待;

 

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

相关文章:

  • 2022-12-23
  • 2021-12-07
  • 2019-12-19
  • 2018-12-19
  • 2022-01-12
  • 2021-06-29
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-02
  • 2021-11-11
  • 2022-12-23
  • 2021-07-04
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案