首先,您必须在placed_order
. fk1.customer_id
要在删除时级联的列。只需更改该列上的外键约束。
尝试这个:
ALTER TABLE placed_order
MODIFY CONSTRAINT fk1_fk_customer_id
FOREIGN KEY (fk1_customer_id) REFERENCES customer(customer_id)
ON DELETE CASCADE;
接下来,您将需要一个BEFORE DELETE
客户触发器,以保持对在placed_order
表中有条目的客户进行审计。
CREATE OR REPLACE TRIGGER trg_customer_delete
BEFORE DELETE ON customer
FOR EACH ROW
REFERENCING new AS new old AS old
AS
cnt INTEGER;
BEGIN
SELECT COUNT(*)
INTO cnt
FROM placed_order
WHERE fk_customer_id = :old.customer_id
IF cnt > 0 THEN
INSERT INTO previous_customer
(customer_id, first_name, last_name, address)
VALUES
(:old.customer_id, :old.first_name, :old.last_name, :old.address);
END IF;
END;
/