开发者

MySQL Trigger to create a row in a different table

I know there must be a simple way to do this but I couldn't find one. I want to create a trigger that basically, when a row is created in table1, creates a n开发者_如何学编程ew row in table2 with a foreign key of the id of table1. what is the general syntax for this? thanks!!


CREATE TRIGGER Syntax

For example -

DELIMITER $$

CREATE TRIGGER trigger1
  AFTER INSERT
  ON table1
  FOR EACH ROW
BEGIN
  INSERT INTO table2(id) VALUES(NEW.id);
END$$

DELIMITER ;


something like this:

CREATE TRIGGER `create_t1` AFTER INSERT ON `table1` FOR EACH ROW BEGIN
INSERT INTO table2
SET t1ID = NEW.ID, 
    when = Now();
END;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜