开发者

MySQL Trigger to create a row in a different table

开发者 https://www.devze.com 2023-03-26 20:35 出处:网络
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 f

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

精彩评论

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

关注公众号