I'm new to using triggers but having read quite a bit on it I think its the best solution to what I need. I have a table in mysql called employees and I want to create an audit trail of changes made to employees into another table called employee_changes. But I only want to create the audit entries for only the columns that have changed in the employees table. So for instance if the user changes the employee's salary, I want a row to be created in employee_changes capturing the ID of the employee, the name of the column that was changed (in this case salary), the old value and new value of the salary as well as the date and name of the user. The name of the user that made the change will always be updated in a field (LastUpdatedBy) in the employees table when the record is saved on the front end, so by the time the trigger is firing, it can get the user name from the LastUpdatedBy column.
With the help of This Post and This Post I was able to come up with this code:
DELIMITER //
CREATE TRIGGER emp_update
AFTER INSERT
ON employees FOR EACH ROW
BEGIN
DECLARE col_names CURSOR FOR
SELECT column_name
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'employees'
ORDER BY ordinal_position;
select FOUND_ROWS() into num_rows;
SET i = 1;
the_loop: LOOP
IF i > num_rows THEN
CLOSE col_names;
LEAVE the_loop;
END IF;
FETCH col_names
INTO col_name;
IF OLD.col_name <> NEW.col_name THEN
INSERT INTO employee_changes (EmployeeNumber, FieldName, OldValue, NewValue, ChangeDate, ChangedBy)
VALUES(OLD.EmployeeNumber, col_name, OLD.col_name, NEW.col_name, NOW(), NEW.LastUpdatedBy);
END IF;
SET i = i + 1;
END LOOP the_loop;
END //
DELIMITER ;
But when I run it, nothing happens, no trigger is created in INFORMATION_SCHEMA.TRIGGERS. And so when I update the employees table, nothing happens in my employee_changes table.
I feel like I'm almost there but I'm stuck.
Thanks for your help.