默认情况下,完整性约束检查在SQL语句执行完以后进行,为什么不在SQL语句执行期间检查呢?
因为在同一时间只有一条语句处理数据才是正常的。
下面演示一下完整性约束的延迟和即时性
[autosys@pderptest ~]$ sqlplus /nolog
SQL*Plus: Release 9.2.0.7.0 - Production on Sat Oct 17 19:52:44 2009
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
SQL> conn jk/aaaaaaaa
Connected.
SQL> create table p(pk int primary key);
Table created.
SQL> create table p1 (fk int constraint p1_fk references p(pk)
2 deferrable --表示p1表可以延迟
3 initially immediate
4 );
Table created.
SQL> insert into p values(1);
1 row created.
SQL> insert into p1 values(1);
1 row created.
SQL> update p set pk=2;
update p set pk=2
*
ERROR at line 1:
ORA-02292: integrity constraint (JK.P1_FK) violated - child record found
SQL> set constraint p1_fk deferred; --设置p1表的约束为延迟
Constraint set.
SQL> update p set pk=2;
1 row updated.
这里没有报错,本质上已经违反约束
那么,我们设置为即时检查约束,
SQL> set constraint p1_fk immediate;
SET constraint p1_fk immediate
*
ERROR at line 1:
ORA-02291: integrity constraint (JK.P1_FK) violated - parent key not found
我们看到,事务是任然是P1_FK约束为deferred运行。马上检查完整
SQL> update p1 set fk=2;
1 row updated.
SQL> set constraint p1_fk immediate;
Constraint set.
SQL> commit;
Commit complete.
SQL>
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/7551038/viewspace-616789/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/7551038/viewspace-616789/