博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ORA-02447: cannot defer a constraint that is not deferrable
阅读量:4959 次
发布时间:2019-06-12

本文共 2248 字,大约阅读时间需要 7 分钟。

一个constraint如果被定义成deferrable那么这个constraints可以在deferred和imediate两种状态相互转换。

deferred只在transaction中有效,也就是只可以在transaction过程中使constraint失效,但如果transaction commit的话,transaction会变成immediate。

1* create table cons_parent (id number(10),name varchar2(10))

SQL> /

Table created.

SQL> create table cons_child (id number(10),name varchar2(10));

Table created.

1* alter table cons_parent add primary key (id)

SQL> /

Table altered.

alter table cons_child add constraints chi_fk_par foreign key (id)

references cons_parent(id)

SQL> alter table cons_child add constraints chi_fk_par foreign key (id)

2 references cons_parent(id)

3 /

Table altered.

一个constraints默认是NOT DEFERRABLE的。

1 select constraint_name||' '||deferrable from all_constraints

2* where constraint_name='CHI_FK_PAR'

SQL> /

CONSTRAINT_NAME||''||DEFERRABLE

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

CHI_FK_PAR NOT DEFERRABLE

NOT DEFERRABLE的不能在deferred和imediate两种状态相互转换

SQL> set constraints chi_fk_par deferred;

SET constraints chi_fk_par deferred

*

ERROR at line 1:

ORA-02447: cannot defer a constraint that is not deferrable

--解决办法:删除该约束后,并重建deferrable约束。

SQL> alter table cons_child drop constraints chi_fk_par;

Table altered.

1 alter table cons_child add constraints chi_fk_par foreign key (id)

2* references cons_parent(id) deferrable

SQL> /

Table altered.

1 select constraint_name||' '||deferrable from all_constraints

2* where constraint_name='CHI_FK_PAR'

SQL> /

CONSTRAINT_NAME||''||DEFERRABLE

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

CHI_FK_PAR DEFERRABLE

一个constraint如果被定义成deferrable那么这个constraints可以在deferred和imediate两种状态相互转换

SQL> set constraints chi_fk_par immediate;

Constraint set.

1* insert into cons_child values (2,'llll')

SQL> /

insert into cons_child values (2,'llll')

*

ERROR at line 1:

ORA-02291: integrity constraint (SYSTEM.CHI_FK_PAR) violated - parent key not found

SQL> set constraints chi_fk_par deferred;

Constraint set.

SQL> insert into cons_child values (2,'llll');

1 row created.

SQL> commit;

commit

*

ERROR at line 1:

ORA-02091: transaction rolled back

ORA-02291: integrity constraint (SYSTEM.CHI_FK_PAR) violated - parent key not found

deferrable会影响CBO的计划,并且正常情况下没有应用的必要,所以建议不要修改,而用系统默认的non deferrable

本篇文章摘自:

转载于:https://www.cnblogs.com/yabingshi/p/3852830.html

你可能感兴趣的文章
Akka(43): Http:SSE-Server Sent Event - 服务端主推消息
查看>>
Developing for nRF52810(转载)
查看>>
POJ2104 K-th Number 主席树
查看>>
线程同步
查看>>
Ubuntu系统OPENGL初体验
查看>>
Java Swing学习
查看>>
HTTP缓存和CDN缓存
查看>>
HDU-1171 Big Event in HDU(生成函数/背包dp)
查看>>
Babel 是干什么的
查看>>
cocos2dx-3.0(8)------Label、LabelTTF、LabelAtlas、LabelBMFont使用之法
查看>>
Mysql数据库乱码总结
查看>>
BZOJ.3160.万径人踪灭(FFT Manacher)
查看>>
CODE[VS] 1842 递归第一次
查看>>
20180418小测
查看>>
Spring Cloud是怎么运行的?
查看>>
12 联结表
查看>>
数字三角形
查看>>
NGUI 减少drawcall规则
查看>>
xss攻击
查看>>
开发环境中快速部署Oracle Essbase(Rapid deployment of oracle essbase in development envrioments)...
查看>>