DECLARE
cursor curs is select * from starting;
appleId number;
bananaId number;
BEGIN
for foo in curs
LOOP
insert into apple (id, weight)
values(1,1)
returning id into appleId;
insert into banana(id,weight)
values(1,3)
returning id into bananaId;
insert into apple_banana_lookup
values(1,appleId,bananaId);
END LOOP;
COMMIT;
END;
上面的代码导致外键约束违反。声称该ID
字段Apple
尚不存在。我的问题是如何使上面的代码起作用并让apple_banana_lookup
表成功地保留appleId
和中引用的键bananaId
。作为补充,我想避免在每次插入后都必须提交,apple
因为banana
给定游标中将有约 2 亿条记录。
更新
架构声明:
create table apple
(
id number(20,0) not null,
weight number (20,0)
);
create table banana
(
id number(20,0) not null,
weight number(20,0)
) ;
create table apple_banana_lookup
(
id number(20,0) not null,
appleId number(20,0) not null,
bananaId number(20,0) not null
CONSTRAINT "apple_fk" foreign key ("appleId")
REFERENCES "apple" ("id"),
CONSTRAINT "banana_fk" foreign key ("bananaId")
REFERENCES "banana" ("id"),
);
错误信息:
ORA-02291: integrity constraint
parent key not found