frigg frigg - 2 months ago 9
MySQL Question

Log when a row is deleted from a given table in MySQL

I have a MySQL-table that's accessed from different APIs. I would like to log when any rows are deleted from this table. Is this possible?

Answer

Yes, you can use a simple trigger, something like;

CREATE TRIGGER data_delete BEFORE DELETE ON data FOR EACH ROW
BEGIN
  INSERT INTO log(logmessage) VALUES (CONCAT('Delete of value ', OLD.id));
END

A simple SQLfiddle demo.