Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using MySQL triggers to log all table changes to a secondary table

Tags:

mysql

triggers

I have a table:

CREATE TABLE `data_table` ( `data_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `field1` INT NOT NULL , `field2` INT NOT NULL , `field3` INT NOT NULL ) ENGINE = MYISAM ; 

I would log to log any chances to field1, 2, or 3 to:

CREATE TABLE `data_tracking` ( `tracking_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `data_id` INT NOT NULL , `field` VARCHAR( 50 ) NOT NULL , `old_value` INT NOT NULL , `new_value` INT NOT NULL , `modified` DATETIME NOT NULL ) ENGINE = MYISAM ;  

I'm using MySQL 5, and I would like to create a trigger to do. I would like to insert a new row into data_tracking anytime data_table is updated, and record the old/updated value, as well as the field changed. I tried the following without any success.

DELIMITER $$  DROP TRIGGER `update_data `$$  CREATE TRIGGER `update_data` AFTER UPDATE on `data_table` FOR EACH ROW BEGIN     IF (NEW.field1 != OLD.field1) THEN         INSERT INTO data_tracking set old_value = OLD.field1, new_value = NEW.field1, field = "field1";     END IF; END$$  DELIMITER ; 

It gave an error on the insert line, I'm not quite sure what the syntax on that should be, or if I'm going about this the right way. Any help would be appreciated. Thanks.

like image 599
Rob Avatar asked Apr 22 '09 21:04

Rob


People also ask

Can we use one trigger in more than one table?

There are no way to link one trigger to two tables unless you create an updatable view which hides both tables map all application code to work with this view.

How do I trigger a track change in MySQL?

To ensure that changes to the data table are logged to the data_log table, we need to create a set of triggers. The triggers need to adhere to the following rules: For inserts, log an insert operation showing the values in the new row. For updates, log an update operation showing the new values in the updated row.

What is a trigger MySQL?

A trigger is a named database object that is associated with a table, and that activates when a particular event occurs for the table. Some uses for triggers are to perform checks of values to be inserted into a table or to perform calculations on values involved in an update.

Which MySQL table Cannot have a trigger?

MySQL triggers cannot: Use SHOW , LOAD DATA , LOAD TABLE , BACKUP DATABASE, RESTORE , FLUSH and RETURN statements.


1 Answers

The insert syntax is

INSERT INTO table (columns_list) VALUES (values_list) 

so your insert would look something like this (i'm not a MySQL expert but you can fit the query):

INSERT INTO data_tracking  (`data_id` , `field` , `old_value` , `new_value` , `modified` )  VALUES  (NEW.data_id, 'field1', OLD.field, NEW.field, CURRENT_DATETIME()); 

And repeat it for variation on field2 and field3

I think this would be the complete trigger, please try:

DELIMITER $$  DROP TRIGGER `update_data `$$  CREATE TRIGGER `update_data` AFTER UPDATE on `data_table` FOR EACH ROW BEGIN     IF (NEW.field1 != OLD.field1) THEN         INSERT INTO data_tracking              (`data_id` , `field` , `old_value` , `new_value` , `modified` )          VALUES              (NEW.data_id, "field1", OLD.field1, NEW.field1, NOW());     END IF;     IF (NEW.field2 != OLD.field2) THEN         INSERT INTO data_tracking              (`data_id` , `field` , `old_value` , `new_value` , `modified` )          VALUES              (NEW.data_id, "field2", OLD.field2, NEW.field2, NOW());     END IF;     IF (NEW.field3 != OLD.field3) THEN         INSERT INTO data_tracking              (`data_id` , `field` , `old_value` , `new_value` , `modified` )          VALUES              (NEW.data_id, "field3", OLD.field3, NEW.field3, NOW());     END IF; END$$  DELIMITER ; 
like image 87
Jhonny D. Cano -Leftware- Avatar answered Oct 04 '22 09:10

Jhonny D. Cano -Leftware-