1

嘿,我在数据库中创建触发器时遇到了一点问题。

我有一个名为“位于”的表,其中包含:

  • 国家
  • 城市

所以 Lake、Sea 和 River 是外键。

我创建了这三个约束:

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;
/

但后来我得到一个变异错误。

目标是删除这三件事为空的每个元组。

也许有人可以帮助我?

提前致谢

4

1 回答 1

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_lake 等,从湖表中删除,然后从定位中删除。
  • 实际上,更有效的是,您应该使用这两种方法中的任何一种,但首先删除行(DELETE FROM located WHERE sea {theSeaBeingDeleted} AND river IS NULL AND lake IS NULL;)。这样你就不会更新一堆你将要删除的行。
于 2014-06-11T23:18:26.643 回答