在给定的表中,我有两条彼此相关的记录。第一个指针指向第二个指针,第二个指针指向第一个指针。要求是删除其中一个,然后通过触发动作删除另一个。
我尝试使用删除第一条记录的after删除
触发器中的第二条记录Delete from table where key = <second record>
认为由于这是“删除后”,当触发器运行以删除第二条记录时,第一条记录将不再存在。但结果是两条记录都没有被删除。 我尝试使用
在第一条记录的before delete 触发器中删除第二条记录的交叉引用
Update table set cross reference to 0 where key = <second record>
第二次调用时停止触发器产生任何影响。仍然没有删除任何记录。
就好像在记录所有操作之前,更改实际上并未应用于数据库,因此第一种方法失败,因为当第二个触发器运行时,第一条记录
仍然存在,并且在第二种情况下,对当第二个触发器运行时,第二条记录尚未应用。谢天谢地,Firebird 识别出了未暗示的循环并停止了。脚本超过2,000,000行,删除记录的情况肯定有一千种,所以请不要让我更改脚本。我已经用谷歌搜索了很多次,但所有要删除的引用要么是简单引用,要么是脚本运行时已知的多个记录。请记住这是 Firebird 2.5。
有人知道如何通过触发器做到这一点吗?
对我来说一切都是开箱即用的:
create table t1 (id1 integer not null primary key);
create table t2 (id2 integer not null primary key);
alter table t1 add id2 integer references t2 (id2) on delete cascade;
alter table t2 add id1 integer references t1 (id1) on delete cascade;
insert into t1 values (1,null);
insert into t1 values (2,null);
insert into t2 values (1,null);
insert into t2 values (2,null);
update t1 set id2=id1;
update t2 set id1=id2;
select * from t1;
ID1 ID2
============ ============
1 1
2 2
select * from t2;
ID2 ID1
============ ============
1 1
2 2
commit;
delete from t1 where id1=1;
select * from t1;
ID1 ID2
============ ============
2 2
select * from t2;
ID2 ID1
============ ============
2 2
delete from t2 where id2=2;
select * from t1;
select * from t2;