当前位置:   article > 正文

Oracle可延迟约束Deferable的使用

oracle 延迟约束检查

标准规定,约束可以是deferrable或not deferrable(默认)。

not deferrable 约束在每一个DML语句后检查;

deferrable 约束可以在每一个insert,delete,或update(即时模式)后立即检查,或者在事务末尾检查(延迟模式)

当没有按特定顺序执行数据加载时,这项功能特别有用——它允许先把数据载入子表,然后再装入父表。

另一种用法是在加载不符合某个check约束的数据之后,对其进行适当的更新。

语法如下:

[ [not] deferrable [initially {immediate | deferred} ] ]

[ [initially {immediate | deferred} ] [not] deferrable ]

deferrable介绍

1.1 deferrable的两个选项区别

deferrable表示该约束是可延迟验证的它有两个选项:

Initially immediate(默认): 立即验证执行完一个sql后就进行验证;

Initially deferred: 延迟验证当事务提交时或调用set constraint[s] immediate语句时才验证.

区别是事务提交时验证不通过则立即回滚事务; set constraint[s] immediate只验证不回滚事务.

1.2 not deferrabledeferrable区别

区别就在于: “立即验证的可延迟约束” 是可以根据需要设置成 延迟验证的可延迟约束不可延迟验证是不能改变的.

deferrable实例

2.1 建表

create table test1(a number(1constraint check_a check(a > 0deferrable

                   initially immediate,

                   b number(1constraint check_b check(b > 0deferrable

                   initially deferred);

2.2 正常插入,没问题

SQL> insert into test1 values(1, 1);

1 row inserted

2.3 检验立即验证:数据不能插入

SQL> insert into test1 values(-1, 1);

insert into test1 values(-1, 1)

ORA-02290: 违反检查约束条件 (MYHR.CHECK_A)

2.4 检验延迟验证:可以执行

SQL> insert into test1 values(1, -1);

1 row inserted

SQL> select * from test1;

 A  B

-- --

 1  1

 1 -1

2.5 提交延迟验证(commit):验证失败,自动回滚

SQL> commit;

 commit

 ORA-02091: 事务处理已回退

ORA-02290: 违反检查约束条件 (MYHR.CHECK_B)

2.6 提交延迟验证(set constraint immediate):验证失败,不回滚

SQL> insert into test1 values(1, -1);

1 row inserted

SQL> set constraint check_b immediate;

set constraint check_b immediate

ORA-02290: 违反检查约束条件 (MYHR.CHECK_B)

或者将所有的约束做修改: alter session set constraints = immediate;

或者:set constraints all immediate;

2.7 将延迟验证设置为立即验证:则在插入时出错

SQL> set constraint check_b immediate;

Constraints set

SQL> insert into test1 values(1,-1);

insert into test1 values(1,-1)

ORA-02290: 违反检查约束条件 (MYHR.CHECK_B)

deferrable用途

3.1 物化视图

物化视图(快照),这是它的主要用途。这些视图会使用延迟约束来进行视图刷新。在刷新物化视图的过程中,可能会破坏完整性,而且将不能逐句检验约束。但到执行COMMIT时,数据完整性就没问题了,而且能满足约束。没有延迟约束,物化视图的约束可能会使刷新过程不能成功进行。

3.2 级联更新

当预测是否需要更新父/子关系中的主键时,它有助于级联更新。看一下实际的例子:

SQL> create table t(tno number(10) constraint pk_t_tno primary key, tname varchar2(20));

SQL> create table s(sno number(10) constraint pk_s_tno primary key, sname varchar2(20), tno number(10));

 

SQL> alter table s add constraint fk_s_tno foreign key (tno) references t(tno);

SQL> insert into t values(1,'yuechaotian');

SQL> insert into t values(2,'tianyuechao');

SQL> commit;

SQL> insert into s values(1,'stu_1', 1);

SQL> insert into s values(2,'stu_2', 1);

SQL> commit;

 

SQL> update t set tno=22 where tno=2;

SQL> update t set tno=11 where tno=1;

update t set tno=11 where tno=1

ORA-02292: 违反完整约束条件 (MYHR.FK_S_TNO) - 已找到子记录

SQL> select * from t;

        TNO TNAME

----------- --------------------

          1 yuechaotian

         22 tianyuechao

 

SQL> select *from s;

        SNO SNAME                        TNO

----------- -------------------- -----------

          1 stu_1                          1

          2 stu_2                          1

 

SQL> rollback;

SQL> alter table s drop constraint fk_s_tno;

SQL> alter table s add constraint fk_s_tno foreign key (tno) references t(tno) deferrable initially immediate;

SQL> select * from t;

        TNO TNAME

----------- --------------------

          1 yuechaotian

          2 tianyuechao

 

SQL> select * from s;

        SNO SNAME                        TNO

----------- -------------------- -----------

          1 stu_1                          1

          2 stu_2                          1

 

SQL> set constraint fk_s_tno deferred;

Constraints set

SQL> update t set tno=22 where tno=2;

SQL> update t set tno=11 where tno=1;

SQL> update s set tno = 11 where tno = 1;

SQL> commit;

SQL> select * from t;

        TNO TNAME

----------- --------------------

         11 yuechaotian

         22 tianyuechao

 

SQL> select * from s;

        SNO SNAME                        TNO

----------- -------------------- -----------

          1 stu_1                         11

          2 stu_2                         11

 

SQL> select a.constraint_name, a.deferrable, a.deferred from user_constraints a where a.constraint_name like '%TNO%';

CONSTRAINT_NAME                DEFERRABLE     DEFERRED

------------------------------ -------------- ---------

FK_S_TNO                       DEFERRABLE     IMMEDIATE

PK_S_TNO                       NOT DEFERRABLE IMMEDIATE

PK_T_TNO                       NOT DEFERRABLE IMMEDIATE

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/14293828/viewspace-1406788/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/14293828/viewspace-1406788/

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/653123
推荐阅读
相关标签
  

闽ICP备14008679号