Search code examples
mysqldatabasetriggersbackup

How to create 2 archives using a TRIGGER?


To create an archive from my table named store, I would like to backup a row and stock it in a specifique table (named:audit) before an update.

And after I would like to backup the same row and stock it in a specifique table (named:histo) after an update.

I thought of a TRIGGER.

Like this, but it does not work because there is 2 INSERT INTO

BEGIN
IF (NEW.storage_0 != OLD.storage_0 OR NEW.storage_1 != OLD.storage_1) 
THEN
INSERT INTO audit(id,Date_insert,name,storage_1,storage_2) VALUES (OLD.id,OLD.Date_insert,OLD.name,OLD.storage_1,OLD.storage_2);
INSERT INTO histo(id,Date_insert,name,storage_1,storage_2) VALUES (NEW.id,NEW.Date_insert,NEW.name,NEW.storage_1,NEW.storage_2);

ELSEIF (NEW.Date_insert IS NULL)
THEN 
INSERT INTO audit(id,Date_insert,name,storage_1,storage_2) VALUES (OLD.id,OLD.Date_insert,OLD.name,OLD.storage_1,OLD.storage_2);
INSERT INTO histo(id,Date_insert,name,storage_1,storage_2) VALUES (NEW.id,NEW.Date_insert,NEW.name,NEW.storage_1,NEW.storage_2);

END IF; 
END

Solution

  • I finally found the problem. Do not use the "id" field if you want to add a new row.