Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct SQL for a trigger to copy a record to an identical table?

I have two tables TABLE1 and TABLE2 with identical structures. I need a trigger to copy a record after insert from TABLE1 to TABLE2. What's the proper SQL for this?

like image 359
Owen Avatar asked Jan 23 '23 04:01

Owen


1 Answers

this would work:

CREATE OR REPLACE TRIGGER copy_tab1_tab2_trg AFTER INSERT ON table1
   FOR EACH ROW
BEGIN
    INSERT INTO TABLE2 
       (col1, col2, ..., coln) 
    VALUES 
       (:NEW.col1, :NEW.col2, ..., :NEW.coln);
END;
like image 153
Vincent Malgrat Avatar answered Jan 26 '23 01:01

Vincent Malgrat