如果tableB.columnZ代表的參考表A記錄的計數,沒有意義觸發TableA上的UPDATE除非表A的參考列可以改變。
首先OCASION:tableA.ReferenceColumn不會改變:
CREATE OR REPLACE TRIGGER test_trig
AFTER INSERT ON tableA
FOR EACH ROW
BEGIN
UPDATE tableB
SET columnZ = columnZ + 1
WHERE tableB.columnX = :NEW.columnX;
END test_trig;
/
二OCASION:tableA.ReferenceColumn確實發生了改變:
CREATE OR REPLACE TRIGGER test_trig
AFTER INSERT OR UPDATE OF columnX ON tableA
FOR EACH ROW
BEGIN
IF UPDATING AND nvl(:OLD.columnX,0) <> 0 THEN
UPDATE tableB
SET columnZ = columnZ - 1
WHERE tableB.columnX = :OLD.columnX;
END IF:
IF nvl(:NEW.columnX,0) <> 0 THEN
UPDATE tableB
SET columnZ = columnZ + 1
WHERE tableB.columnX = :NEW.columnX;
END IF;
END test_trig;
/
三OCASION:tablaA記錄可以刪除:
CREATE OR REPLACE TRIGGER test_trig
AFTER INSERT OR DELETE OR UPDATE OF columnX ON tableA
FOR EACH ROW
BEGIN
IF (UPDATING OR DELETING) AND nvl(:OLD.columnX,0) <> 0 THEN
UPDATE tableB
SET columnZ = columnZ - 1
WHERE tableB.columnX = :OLD.columnX;
END IF:
IF nvl(:NEW.columnX,0) <> 0 THEN
UPDATE tableB
SET columnZ = columnZ + 1
WHERE tableB.columnX = :NEW.columnX;
END IF;
END test_trig;
/