I am unable to get the following code to work, using this page for reference as well as of other posts on this site. I need to create a trigger that will insert a record in Table B whenever Table A is updated. The code below shows what I am attempting; however this produces a syntax error (#1064). I am also unclear on if I need to include the ‘DELIMITER $$’ syntax or not. I appreciate your help
DELIMITER $$
CREATE TRIGGER MyTrigger
AFTER INSERT
ON TableA
FOR EACH ROW
BEGIN
INSERT INTO TableB SET
TableA_id = NEW.TableB_id,
TableA_date = NEW.TableB_date,
TableA_comment = NEW.TableB_comment;
END;
END $$
DELIMITER ;
EDIT: in the pseudo-code above I am using a $TableName_$FieldName convention to indicate that Column A belongs to Table A, Column B belongs to Table B. I should have made that more clear in my original question. Someone commented below I have the NEW indicator on the wrong side (should be on Table A), but that comment appears to have been removed. Can someone please confirm? Thanks for all your help
Try this:
The
DELIMITERhere is used to tell mysql to treat all the following;as part of the definition instead of actual command terminations.Notice that I removed an unmatched
END;just before the closingEND $$