I have this trigger below which inserts new values in the an emplog
table. I want to have my trigger insert in the description column the event that took place. For example, if I changed the last_name I want it to record:
old.lastname
was changed tonew.last_name
If its the first name or gender or DOB or marital or SSN that were updated it should do the same.
How can I achieve this? Can I use the concat
function and loops?
CREATE TRIGGER emplog_update AFTER UPDATE ON emp
FOR EACH ROW
INSERT INTO emplog
VALUES (
NEW.id
,NEW.lastname
,NEW.firstname
,NEW.gender
,NEW.dob
,NEW.marital
,NEW.SSN
,'U'
,NULL
,USER()
,('this is where the description will go')
);
Best Answer
How about expanding this a little and do a proper log table, logging every update, insert or delete. That way you can keep a proper history. For this, in MySQL, you will need three triggers :
If you then wish to check out the history of what happened for a specific employee, you simply run this query :