嘿,我在我的数据库中创建一个触发器有点问题。
我有一张名为'位于'并包含:
所以湖泊,海洋和河流都是外键。
我创建了这三个约束:
ALTER TABLE located
ADD CONSTRAINT locatedLake
FOREIGN KEY (lake)
REFERENCES Lake(name)
ON DELETE SET NULL;
ALTER TABLE located
ADD CONSTRAINT locatedRiver
FOREIGN KEY (river)
REFERENCES River(name)
ON DELETE SET NULL;
ALTER TABLE located
ADD CONSTRAINT locatedSea
FOREIGN KEY (sea)
REFERENCES Sea(name)
ON DELETE SET NULL;
因此,每当我删除位于它的湖泊,河流或海洋时,它都会设置为NULL。
现在我尝试创建此触发器:
CREATE OR REPLACE TRIGGER deleteFromLocated
AFTER UPDATE ON located
FOR EACH ROW
BEGIN
DROP FROM located WHERE sea IS NULL AND river IS NULL AND lake IS NULL;
END;
/
然后我得到了一个误导错误。
目标是删除这三个元素为空的每个元组。
也许有人可以帮助我?
提前致谢
答案 0 :(得分:1)
首先,FOR EACH ROW
对您没有帮助,因为您没有使用:new
或:old
来处理正在更新的特定行。您可以尝试删除FOR EACH ROW
。 (我现在没有可以测试的环境对不起)
实际上这似乎不是一个好用的触发器。你可以改为:
DELETE FROM located WHERE sea IS NULL AND river IS NULL AND lake IS NULL;
DELETE FROM located WHERE sea {theSeaBeingDeleted} AND river IS NULL AND lake IS NULL;
)。这样你就不会更新你要删除的一堆行。