I’m trying to create some triggers for my MySQL tables to keep track of changes. I thought of a table like
CREATE TABLE IF NOT EXISTS `contacts_changes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`action` enum('insert','update','delete') NOT NULL,
`contact_id` int(6) NOT NULL,
`changes` text NOT NULL,
PRIMARY KEY (`id`)
);
where contact_id is a foreign key to the table I want to monitor and in column changes I want to store the changes made using the JSON format.
So if I change the familyname and birthday of an entry in the contact table the changes column should contain {"familyname": "Smith", "birthday": "1982-06-24}.
I found a lot of examples for using triggers to track changes but none of them summarizes the changes made in a single column of a single row. What they do is to insert a new row for each change made, i.e. one row for changing the birthday and another for changing the familyname.
Due to my lack of knowledge of the (My)SQL language I failed to figure out if it’s possible to do something I had in mind and if it is how it can be done.
Any ideas or hints how to solve this?
Thanks!
Edit: Posted former edit as answer.
Alright, solved it myself. Wasn’t that hard … 😉