oracle:推迟外键检查不起作用

问题描述 投票:1回答:1

oracle:推迟外键检查不起作用。例如,

create table Foo (id number(20,0), name varchar(20), 
    primary key(id));
create table Bar (id number(20,0), name varchar(20), 
    primary key(id),
    constraint FK1 foreign key (id) references Foo (id));

insert into Foo(id,name) values(1, 'foo');
insert into Bar(id,name) values(1, 'bar');

删除数据:

SET CONSTRAINTS ALL DEFERRED;

delete from Foo;
delete from Bar;

SET CONSTRAINTS ALL IMMEDIATE;

错误:

ORA-02292: integrity constraint violated - child record found
oracle foreign-keys
1个回答
1
投票

如果为NOT DEFERRABLE,则不能延迟约束。约束可以是可推迟的,也可以是不可推迟的,但是默认值(如果您未明确指定任何一种方式)是NOT DEFERRABLE

在您的示例代码中,在外键约束定义之后添加关键字deferrable,然后再次运行所有内容。它将按预期工作。

即:编辑下面的代码行

constraint FK1 foreign key (id) references Foo (id));

to

constraint FK1 foreign key (id) references Foo (id) deferrable);
© www.soinside.com 2019 - 2024. All rights reserved.