I have the following two tables:
CREATE TABLE entries
(
id NUMBER(20, 0) NOT NULL PRIMARY KEY,
first_name VARCHAR2(512) DEFAULT NULL NULL,
last_name VARCHAR2(512) DEFAULT NULL NULL,
last_updated_at TIMESTAMP DEFAULT NULL NULL
);
CREATE TABLE entry_details
(
id NUMBER(20, 0) NOT NULL PRIMARY KEY,
entry_id NUMBER(20, 0) NOT NULL,
value VARCHAR2(512) DEFAULT NULL NULL,
CONSTRAINT fk_details_entry_id FOREIGN KEY (entry_id) REFERENCES entries(id) ON DELETE CASCADE
);
I need to update last_updated_at
whenever there's a change/delete in the entries
or entry_details
table, so I have this trigger:
CREATE OR REPLACE TRIGGER detail_delete_trigger
BEFORE DELETE
ON entry_details
FOR EACH ROW
BEGIN
UPDATE entries
SET last_updated_at = CURRENT_TIMESTAMP
WHERE id = :OLD.entry_id;
END;
This works fine when there's a delete from entry_details
directly. However, the I get the
ORA-04091: entries table is mutating
error when there's delete from entries
table which cascades to entry_details
. I have tried:
PRAGMA AUTONOMOUS_TRANSACTIONS
which yield toORA-00060: deadlock detected while waiting for resource
- Using a
AFTER DELETE
trigger, which gave again, the table is mutating error - Could not use
INSTEAD OF DELETE
because it's not for tables as perORA-25002: cannot create INSTEAD OF triggers on tables
- I removed
CASCADE
and added the following trigger:
ALTER TABLE entry_details DROP CONSTRAINT fk_details_entry_id;
ALTER TABLE entry_detailsADD CONSTRAINT fk_details_entry_idFOREIGN KEY (entry_id) REFERENCES entries(id);
CREATE OR REPLACE TRIGGER aml_entries_delete_trigger
BEFORE DELETE -- or AFTER DELETE, same result
ON entries
FOR EACH ROW
BEGIN
DELETE FROM entry_details WHERE entry_id = :OLD.id;
END;
But I still get the ORA-04091 table is mutating trigger/function may not see
error. How do I overcome this?