I have the following table:
CREATE TABLE individuo(
codigo NUMBER(8) PRIMARY KEY,
nombre VARCHAR2(20) NOT NULL,
valor NUMBER(8) NOT NULL CHECK (valor > 0),
padre NUMBER(8) REFERENCES individuo,
nro_hijos NUMBER(8) NOT NULL CHECK (nro_hijos >=0),
CHECK(padre <> codigo)
);
Where padre means father and nro_hijos means number of children.
I need a trigger that when I insert or delete a row where padre IS NOT NULL, it increases or decreases the nro_hijos correspondingly.
I tried this:
CREATE OR REPLACE TRIGGER individuo_parent_increment
AFTER INSERT OR DELETE ON INDIVIDUO FOR EACH ROW WHEN (NEW.padre IS NOT NULL)
BEGIN
IF INSERTING THEN
UPDATE INDIVIDUO SET NRO_HIJOS = NRO_HIJOS + 1 WHERE CODIGO = :NEW.PADRE;
END IF;
END;
but it thew ORA-04091 (table individuo is mutating, trigger/function may not see it)
I also tried using a COMPOUND TRIGGER but same error occured.
I have a different trigger already setup:
CREATE OR REPLACE TRIGGER individuo_initial_children
BEFORE INSERT ON INDIVIDUO
FOR EACH ROW
BEGIN
:NEW.NRO_HIJOS := 0;
END;