I have DB of selling tickets for trains. I have table Ticket
and table Cancel
(here are canceled tickets). I have already written a trigger, when if I add some ticket to table Cancel
then this ticket sets free in table Ticket(Client_id attribute = NULL).
But I want to write new one: "if I have canceled ticket(Client_id = NULL in table Ticket), and then when I update ticket and sets Client_id = 'some_number'(e.g. Client_id = 5), then I should delete this ticket from Cancel
table.
Here is my trigger:
CREATE OR REPLACE TRIGGER Buy_Cancel
AFTER UPDATE
ON TICKET
FOR EACH ROW
WHEN(old.CLIENT_ID IS NULL)
DECLARE
rowAmmount INTEGER;
BEGIN
SELECT COUNT(*)
INTO rowAmmount
FROM CANCEL c JOIN TICKET t ON c.TICKET_ID = t.TICKET_ID
WHERE t.TICKET_ID = :OLD.TICKET_ID;
IF rowAmmount > 0 THEN
DELETE FROM CANCEL c
WHERE c.TICKET_ID = :NEW.TICKET_ID;
END IF;
END;
When I run a query:
UPDATE TICKET t
SET t.CLIENT_ID = 5
WHERE t.TICKET_ID = 2;
it cause such errors:
ORA-04091: table MYDB.TICKET is mutating, trigger/function may not see it
ORA-06512: at "MYDB.BUY_CANCEL", line 4
ORA-04088: error during execution of trigger 'MYDB.BUY_CANCEL' SQL6.sql 2 8