ORACLE触发器检查表和删除行

时间:2014-06-11 21:25:12

标签: sql database oracle triggers mutating-table

嘿,我在我的数据库中创建一个触发器有点问题。

我有一张名为'位于'并包含:

  • 国家

所以湖泊,海洋和河流都是外键。

我创建了这三个约束:

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

然后我得到了一个误导错误。

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

也许有人可以帮助我?

提前致谢

1 个答案:

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