我有以下表格。
ACCOUNTS(父表),其列为ACC_ID,VALID_FROM和VALID_TO,ACC_ID,VALID_FROM上有唯一键。
ACCOUNT_BUSII_FUNCTIONS(子表),其中列为ACC_ID,BUSINESS_FUNCTION,VALID_FROM和VALID_TO,ACC_ID,BUSINESS_FUNCTION,VALID_FROM上有唯一键。
VALID_FROM和VALID_TO是日期。
我需要建立一个关系,其中在ACCOUNTS(父表)中删除的每个记录都应检查属于同一日期范围的子记录。同样,在插入子级之前,请检查父级是否存在有效日期范围。
显然,我不能使用外键约束,因为涉及日期范围。
尝试编写函数并调用CHECK约束,但由于CHECK约束不允许用户定义函数,因此无效。
无能为力......请帮助......
干杯 TZH
答案 0 :(得分:0)
我认为触发器比功能更好。尝试类似:
CREATE TRIGGER ACCOUNTS_BD
BEFORE DELETE ON ACCOUNTS
FOR EACH ROW
DECLARE
nChild_rows NUMBER:
BEGIN
SELECT COUNT(*)
INTO nChild_rows
FROM ACCOUNT_BUSII_FUNCTIONS a
WHERE a.ACC_ID = :OLD.ACC_ID AND
(:OLD.VALID_FROM BETWEEN a.VALID_FROM AND a.VALID_TO OR
:OLD.VALID_TO BETWEEN a.VALID_FROM AND a.VALID_TO OR
a.VALID_FROM BETWEEN :OLD.VALID_FROM AND :OLD.VALID_TO OR
a.VALID_TO BETWEEN :OLD.VALID_FROM AND :OLD.VALID_TO);
IF nChild_rows > 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'Child rows found when deleting from ACCOUNTS');
END IF;
END ACCOUNTS_BD;
CREATE TRIGGER ACCOUNT_BUSII_FUNCTIONS_BI
BEFORE INSERT ON ACCOUNT_BUSII_FUNCTIONS
FOR EACH ROW
DECLARE
nParent_rows NUMBER;
BEGIN
SELECT COUNT(*)
INTO nParent_rows
FROM ACCOUNTS a
WHERE a.ACC_ID = :NEW.ACC_ID AND
(:NEW.VALID_FROM BETWEEN a.VALID_FROM AND a.VALID_TO OR
:NEW.VALID_TO BETWEEN a.VALID_FROM AND a.VALID_TO OR
a.VALID_FROM BETWEEN :NEW.VALID_FROM AND :NEW.VALID_TO OR
a.VALID_TO BETWEEN :NEW.VALID_FROM AND :NEW.VALID_TO);
IF nParent_rows = 0 THEN
RAISE_APPLICATION_ERROR(-20002, 'No parent row found when inserting into ' ||
'ACCOUNT_BUSII_FUNCTIONS');
END IF;
END ACCOUNT_BUSII_FUNCTIONS_BI;
未在动物身上进行测试 - 您将成为第一个! : - )
分享并享受。