- 最后登录
- 2017-9-10
- 在线时间
- 31 小时
- 威望
- 0
- 金钱
- 101
- 注册时间
- 2013-11-19
- 阅读权限
- 10
- 帖子
- 18
- 精华
- 0
- 积分
- 0
- UID
- 1402
|
1#
发表于 2014-5-8 10:27:40
|
查看: 4629 |
回复: 3
刘大,你好,请指出我的 有误之处
硬件环境:Redhat 5.4-32bit
ORACLE: 10.2.0.2-32bit
ORACLE 官方文档上有介绍:有主外键关系的两张表,当子表外键上没有索引时,对主表做更新、删除操作,会锁住子表!
但是我的测试结果不是这样,以下附上我的测试 方案及 测试结果 ,请刘大指出我的失误之处。 非常感谢
创建测试环境
TEST @ prod >create table father as select * from scott.dept;
Table created.
TEST @ prod >create table childen as select * from scott.emp;
Table created.
TEST @ prod >alter table father add constraint pk_deptno primary key (deptno);
Table altered.
TEST @ prod >alter table childen add constraint fk_deptno foreign key (deptno) references father(deptno);
Table altered.
测试一:子表的外键 没有 索引!!!
父表插入数据,观察该对象是否被锁
TEST @ prod >insert into father values(50,'beijing','china');
1 row created.
TEST @ prod >select lock1.object_id,obj.object_name,lock1.session_id,lock1.locked_mode from v$locked_object lock1 ,dba_objects obj where lock1.session_id=159 and lock1.object_id=obj.object_id
OBJECT_ID OBJECT_NAM SESSION_ID LOCKED_MODE
--------- ---------- ---------- -----------
52786 CHILDEN 159 2
52785 FATHER 159 3
结论:INSERT操作会同时锁定 父表、子表
子表插入数据,观察该对象是否被锁
TEST @ prod >insert into childen values(1111,'test','test',8888,'28-SEP-81',8,0,50);
1 row created.
TEST @ prod >select lock1.object_id,obj.object_name,lock1.session_id,lock1.locked_mode from v$locked_object lock1 ,dba_objects obj where lock1.session_id=159 and lock1.object_id=obj.object_id;
OBJECT_ID OBJECT_NAM SESSION_ID LOCKED_MODE
--------- ---------- ---------- -----------
52786 CHILDEN 159 3
52785 FATHER 159 2
结论:子表插入数据,会同时锁定 父表、子表
父表更新数据,观察该对象是否被锁
TEST @ prod >update father set deptno=deptno*1 ;
5 rows updated.
TEST @ prod >select lock1.object_id,obj.object_name,lock1.session_id,lock1.locked_mode from v$locked_object lock1 ,dba_objects obj where lock1.session_id=159 and lock1.object_id=obj.object_id;
OBJECT_ID OBJECT_NAM SESSION_ID LOCKED_MODE
--------- ---------- ---------- -----------
52785 FATHER 159 3
结论:Update操作,只会锁定父表不会锁定子表
子表更新数据,观察该对象是否被锁
TEST @ prod >update childen set deptno=deptno*1 ;
14 rows updated.
TEST @ prod >select lock1.object_id,obj.object_name,lock1.session_id,lock1.locked_mode from v$locked_object lock1 ,dba_objects obj where lock1.session_id=159 and lock1.object_id=obj.object_id;
OBJECT_ID OBJECT_NAM SESSION_ID LOCKED_MODE
--------- ---------- ---------- -----------
52786 CHILDEN 159 3
52785 FATHER 159 2
结论:在子表执行 update操作,会同时锁定 父表、子表
父表删除数据,观察该对象是否被锁
TEST @ prod >delete from father where deptno=50;
1 row deleted.
TEST @ prod >select lock1.object_id,obj.object_name,lock1.session_id,lock1.locked_mode from v$locked_object lock1 ,dba_objects obj where lock1.session_id=159 and lock1.object_id=obj.object_id;
OBJECT_ID OBJECT_NAM SESSION_ID LOCKED_MODE
--------- ---------- ---------- -----------
52785 FATHER 159 3
结论:在父表执行Delete操作不提交,只会锁定父表不会锁定子表
子表删除数据,观察对象是否被锁
TEST @ prod >delete from childen where deptno=50;
0 rows deleted.
TEST @ prod >select lock1.object_id,obj.object_name,lock1.session_id,lock1.locked_mode from v$locked_object lock1 ,dba_objects obj where lock1.session_id=159 and lock1.object_id=obj.object_id;
OBJECT_ID OBJECT_NAM SESSION_ID LOCKED_MODE
--------- ---------- ---------- -----------
52786 CHILDEN 159 3
52785 FATHER 159 2
结论:在子表执行delete操作不提交,会同时锁定 父表、子表
测试二:
子表的外键字段添加索引
TEST @ prod >create index ind_deptno on childen(deptno);
Index created.
TEST @ prod >analyze table childen compute statistics;
Table analyzed.
父表插入数据,观察该对象是否被锁???
TEST @ prod >insert into father values(50,'beijing','china');
1 row created.
TEST @ prod >col object_id for 99999
TEST @ prod >col object_name for a10
TEST @ prod >select lock1.object_id,obj.object_name,lock1.session_id,lock1.locked_mode from v$locked_object lock1 ,dba_objects obj where lock1.session_id=159 and lock1.object_id=obj.object_id
OBJECT_ID OBJECT_NAM SESSION_ID LOCKED_MODE
--------- ---------- ---------- -----------
52786 CHILDEN 159 2
52785 FATHER 159 3
结论:INSERT操作会同时锁定 父表、子表
子表插入数据,观察该对象是否被锁
TEST @ prod >insert into childen values(1111,'test','test',8888,'28-SEP-81',8,0,50);
1 row created.
TEST @ prod >select lock1.object_id,obj.object_name,lock1.session_id,lock1.locked_mode from v$locked_object lock1 ,dba_objects obj where lock1.session_id=159 and lock1.object_id=obj.object_id;
OBJECT_ID OBJECT_NAM SESSION_ID LOCKED_MODE
--------- ---------- ---------- -----------
52786 CHILDEN 159 3
52785 FATHER 159 2
结论:子表插入数据,会同时锁定 父表、子表
父表更新数据,观察该对象是否被锁????
TEST @ prod >update father set deptno=deptno*1 ;
5 rows updated.
TEST @ prod >select lock1.object_id,obj.object_name,lock1.session_id,lock1.locked_mode from v$locked_object lock1 ,dba_objects obj where lock1.session_id=159 and lock1.object_id=obj.object_id;
OBJECT_ID OBJECT_NAM SESSION_ID LOCKED_MODE
--------- ---------- ---------- -----------
52785 FATHER 159 3
结论:Update操作不提交 ,只会锁定父表不会锁定子表
子表更新数据,观察该对象是否被锁
TEST @ prod >update childen set deptno=deptno*1 ;
14 rows updated.
TEST @ prod >select lock1.object_id,obj.object_name,lock1.session_id,lock1.locked_mode from v$locked_object lock1 ,dba_objects obj where lock1.session_id=159 and lock1.object_id=obj.object_id;
OBJECT_ID OBJECT_NAM SESSION_ID LOCKED_MODE
--------- ---------- ---------- -----------
52786 CHILDEN 159 3
52785 FATHER 159 2
结论:在子表执行 update操作,会同时锁定 父表、子表
父表删除数据,观察该对象是否被锁
TEST @ prod >delete from father where deptno=50;
1 row deleted.
TEST @ prod >select lock1.object_id,obj.object_name,lock1.session_id,lock1.locked_mode from v$locked_object lock1 ,dba_objects obj where lock1.session_id=159 and lock1.object_id=obj.object_id;
OBJECT_ID OBJECT_NAM SESSION_ID LOCKED_MODE
--------- ---------- ---------- -----------
52786 CHILDEN 159 2
52785 FATHER 159 3
结论:在父表执行 delete操作,会同时锁定 父表、子表
子表删除数据,观察对象是否被锁
TEST @ prod >delete from childen where deptno=50;
0 rows deleted.
TEST @ prod >select lock1.object_id,obj.object_name,lock1.session_id,lock1.locked_mode from v$locked_object lock1 ,dba_objects obj where lock1.session_id=159 and lock1.object_id=obj.object_id;
OBJECT_ID OBJECT_NAM SESSION_ID LOCKED_MODE
--------- ---------- ---------- -----------
52786 CHILDEN 159 3
52785 FATHER 159 2
结论:在子表执行 delete操作,会同时锁定 父表、子表
结论:
子表外键 未创建索引
INSERT UPDATE DELETE
父表 锁父表、子表 锁父表 锁父表
子表 锁父表、子表 锁父表、子表 锁父表、子表
子表外键 创建索引
INSERT UPDATE DELETE
父表 锁父表、子表 锁父表 锁父表、子表
子表 锁父表、子表 锁父表、子表 锁父表、子表
我得出的结论和官方文档上的结论不一致,望刘大指正我的错误之处
谢谢了
|
|