首页 > 数据库 > Oracle > 主从表,增,删,改操作时产生的锁演示
SQL> create table p(id int primary key);
表已创建。
SQL> create table c(id references p(id));
表已创建。
SQL> desc p;
名称 是否为空? 类型
----------------------------------------- -------- ---------------------------
ID NOT NULL NUMBER(38)
SQL> desc c;
名称 是否为空? 类型
----------------------------------------- -------- ---------------------------
ID NUMBER(38)
SQL> insert into p values(1);
已创建 1 行。
SQL> commit;
提交完成。
SQL> update p set id=1 where id=1;
已更新 1 行。
SQL> commit;
提交完成。
SQL> delete from p where id=1;
已删除 1 行。
SQL> rollback;
回退已完成。
SQL> select * from p;
ID
----------
1
SQL> insert into c values(1);
已创建 1 行。
SQL> commit;
提交完成。
SQL> update c set id=1 where id=1;
已更新 1 行。
SQL> commit;
提交完成。
SQL> delete from c where id=1;
已删除 1 行。
SQL> rollback;
回退已完成。
对应在主,从表上进行新增,修改,删除操作时,在主,从表上产生的表锁,行锁具体情况如下:
SQL> conn scott/tiger
已连接。
SQL> select sid,type,id1,id2,lmode,request,block from sys.v$lock where type in (
'TM','TX');
SQL> select sid,type,id1,id2,lmode,request,block from v$lock where type in ('TM'
,'TX');
SID TYPE ID1 ID2 LMODE REQUEST BLOCK
---------- ---- ---------- ---------- ---------- ---------- ----------
72 TM 73901 0 3 0 0
72 TM 73903 0 3 0 0
72 TX 655362 730 6 0 0
SQL> select object_name from dba_objects where object_id in (73901,73903);
OBJECT_NAME
--------------------------------------------------------------------------------
P
C
SQL> select sid,type,id1,id2,lmode,request,block from v$lock where type in ('TM'
,'TX');
未选定行
SQL> select sid,type,id1,id2,lmode,request,block from v$lock where type in ('TM'
,'TX');
SID TYPE ID1 ID2 LMODE REQUEST BLOCK
---------- ---- ---------- ---------- ---------- ---------- ----------
72 TM 73901 0 3 0 0
72 TX 262162 736 6 0 0
SQL> select sid,type,id1,id2,lmode,request,block from v$lock where type in ('TM'
,'TX');
SID TYPE ID1 ID2 LMODE REQUEST BLOCK
---------- ---- ---------- ---------- ---------- ---------- ----------
72 TM 73901 0 3 0 0
72 TX 65550 732 6 0 0
SQL> select sid,type,id1,id2,lmode,request,block from v$lock where type in ('TM'
,'TX');
未选定行
SQL> select sid,type,id1,id2,lmode,request,block from v$lock where type in ('TM'
,'TX');
SID TYPE ID1 ID2 LMODE REQUEST BLOCK
---------- ---- ---------- ---------- ---------- ---------- ----------
72 TM 73901 0 3 0 0
72 TM 73903 0 3 0 0
72 TX 393217 951 6 0 0
SQL> select sid,type,id1,id2,lmode,request,block from v$lock where type in ('TM'
,'TX');
未选定行
SQL> select sid,type,id1,id2,lmode,request,block from v$lock where type in ('TM'
,'TX');
SID TYPE ID1 ID2 LMODE REQUEST BLOCK
---------- ---- ---------- ---------- ---------- ---------- ----------
72 TM 73901 0 3 0 0
72 TM 73903 0 3 0 0
72 TX 131097 903 6 0 0
SQL> select sid,type,id1,id2,lmode,request,block from v$lock where type in ('TM'
,'TX');
未选定行
SQL> select sid,type,id1,id2,lmode,request,block from v$lock where type in ('TM'
,'TX');
SID TYPE ID1 ID2 LMODE REQUEST BLOCK
---------- ---- ---------- ---------- ---------- ---------- ----------
72 TM 73901 0 3 0 0
72 TM 73903 0 3 0 0
72 TX 262155 736 6 0 0
SQL> select sid,type,id1,id2,lmode,request,block from v$lock where type in ('TM'
,'TX');
未选定行
结论:
1:在主表上进行insert 操作时,会在主从,表上增加一个表琐,以及在主表上增加一个行锁。
2:在主表上进行update,delete操作时,会在主表上增加一个表锁,但是从表上不会增加表锁,相应主表上也会增加一个行锁。
3:在从表上进行insert,update,delete操作时,会在主从,表上增加一个表琐,以及在从表上增加一个行锁。
具体为啥会这样,我也还没搞清楚,欢迎大家一起讨论,哈哈。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/14766526/viewspace-1134555/ ,如需转载,请注明出处,否则将追究法律责任。