使用脚本删除级联

时间:2014-03-19 13:30:02

标签: oracle jdbc

我有3个表,这些表不是使用 ON DELETE CASCADE 选项创建的,也不是创建它们的选项。

我可能需要连续删除所有三个表。有没有办法只使用promotion_id作为键来做到这一点?因为我需要以相反的顺序删除,所以当我到达从属表时,promotion_id就消失了。

我认为唯一的方法是使用 JOIN SELECT 3个表的键,然后单独使用它们。但如果有一个纯粹的SQL解决方案,那就太好了。

我正在使用JDBC,Spring和Oracle。感谢。

创建表test_rates(       rate_id varchar2(10)主键,       费率号码     );

create table test_offers (
  offer_id varchar2(10) primary key ,
  rate_id varchar2(10),
  foreign key (rate_id) references test_rates (rate_id)
);

create table test_promotions (
  promotion_id varchar2(10) primary key ,
  offer_id varchar2(10),
  foreign key (offer_id) references test_offers (offer_id)
);

insert into test_rates (rate_id,rate) values (1,199);
insert into test_offers (offer_id,rate_id) values (11,1);
insert into test_promotions (promotion_id,offer_id) values (21,11);

commit;

delete from test_promotions where promotion_id = 21;

delete from test_offers where offer_id in (select offer_id from test_promotions where promotion_id = 1);  -- key is gone by now

1 个答案:

答案 0 :(得分:0)

在一般情况下,如果单个优惠有N个促销(N> 1),如果仅删除一个促销,则删除优惠是没有意义的。你最终会得到孤立的促销活动。

如果您想删除费率,首先删除所有子项promotions然后所有子项offers然后删除rate是有意义的。但在这种情况下,rate_id可以在此过程中使用。

如果删除子记录,则无需删除父记录,除非这是一项要求,在这种情况下,首先要查找父ID并查看上述内容。