剥夺外键约束来化解输入数据出错的问题之解决方案三

老三栽缓解方案:采用Oracle 可缓约束Deferable。

约好是deferrable或not deferrable(默认)。
not deferrable 约束在各国一个DML语句后检查;

deferrable
约束好在列一个insert,delete,或update(即经常模式)后这检查,或者以事情末尾检查(延迟模式)

当没遵循一定顺序执行多少加载时,这项功能特别有因此——它同意先行将数量载入子表,然后再装父表。

另外一样栽用法是在加载不抱某个check约束之数码后,对那个进行适度的翻新

语法如下:

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

或

[ [initially {immediate | deferred} ] [not] deferrable ]
  • deferrable介绍

deferrable的简单独挑选区别

deferrable表示该约束是可延迟验证的. 它发零星独挑选:
Initially immediate(默认): 立即说明, 执行了一个sql后便开展求证;
Initially deferred: 延迟验证, 当事务提交时还是调用set constraint[s]
immediate语词时才验证.
分是: 事务提交时证实不经, 则随即回滚事务; set constraint[s]
immediate时独自验证, 不掉滚事务.

not deferrable与deferrable区别
有别于就在: “立即说明的而延缓约束” 是可因需要装成
“延迟验证的而是延缓约束”的, 而“不可延迟验证”是无能够转的.

  • deferrable实例

建表

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

                   initially immediate,

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

                   initially deferred);

健康插入,没问题

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

1 row inserted

查实立即说明:数据未克插入

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

insert into test1 values(-1, 1)

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

查查延迟验证:可以尽

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

1 row inserted

SQL> select * from test1;

 A  B

-- --

 1  1

 1 -1

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

SQL> commit;

 commit

 ORA-02091: 事务处理已回退

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

交给延迟证(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;

以推验证设置为就说明:则于插入时错

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用途

物化视图

物化视图(快照),这是它的主要用途。这些视图会利用延缓约束来开展视图刷新。在刷新物化视图的长河遭到,可能会见毁完整性,而且以无克挨个句检验约束。但顶实践COMMIT时,数据完整性就没问题了,而且能满足约束。没有延迟约束,物化视图的约或会见要基础代谢过程未可知学有所成开展。

级联更新

当预测是否要更新父/子关系中之主键时,它促进级联更新。看一下实在的例证:

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

```

相关文章