当select...from...where...for update时,where条件能精确到行既为行锁,不能则为表锁。
例1:某张表id是主键(既:id唯一), 此时select…from…where id=3 for update就是行锁
,因为id=3在这张表永远只有一个。
例2:某张表的字段name(name不唯一),此时select…from…where name=张三 for update就是表锁
,因为表中可能存在多个张三。
特殊情况:需要特别注意的是,当字段唯一,也要求where条件要能够找到相应的值才是锁行,否则也会锁表。
例如:某张表id是主键(既:id唯一), 此时select…from…where id=99999999 for update, 然而你的数据id并没有99999999,这样就会锁表
。
参考教程:https://www.bilibili.com/video/av799680821/?p=19&vd_source=031e588a691688b8060b22e0067dc178