In UPDATE TRIGGER you can use the OLD keyword to access row data that is replaced by the update. The NEW keyword allows you to access the data of the incoming line, which, if successful, will replace the old line.
Example UPDATE trigger:
CREATE TRIGGER upd_check AFTER UPDATE ON SomeTable FOR EACH ROW BEGIN IF (OLD.LastChangedBy <> NEW.LastChangedBy) THEN INSERT INTO AuditSomeTable(ID, LastChangedBy) VALUES (OLD.ID, OLD.LastChangedBy); END IF; END;
Sqlfiddle here
Depending on the type of trigger created, OLD and NEW lines may not be available:
INSERT TRIGGER
- Access only to
NEW pseudo-orders.
UPDATE TRIGGER
- Access to
NEW and OLD pseudo-sequences
DELETE TRIGGER
- Access
OLD pseudo-rows only
those. there is no OLD line in the INSERT trigger, and there is no NEW line in the DELETE trigger.
OP Question
The OP did not provide the actual code and error message indicated in the comments:
INSERT trigger missing old row
indicates that the OP unintentionally created an INSERT TRIGGER and not an UPDATE TRIGGER as indicated in the question. An INSERT trigger does not have a pseudo OLD table.
source share