Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to create a trigger in mysql that will insert in to another table when an insert on a table occurs.

CREATE TRIGGER addCardForNewUser AFTER INSERT ON swiped.Users
FOR EACH ROW
  BEGIN
      INSERT INTO swiped.Card (userid) VALUES (get value from original insert here);
  END

In the values part of the insert statement how would i get a value from the original insert to use here?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
999 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use those values with new.columnname. If the name of the column is userid too, then you can use:

CREATE TRIGGER addCardForNewUser AFTER INSERT ON swiped.Users
FOR EACH ROW
  BEGIN
      INSERT INTO swiped.Card (userid) VALUES (new.userid);
  END

From the official documentation:

Within the trigger body, the OLD and NEW keywords enable you to access columns in the rows affected by a trigger. OLD and NEW are MySQL extensions to triggers; they are not case sensitive.

In an INSERT trigger, only NEW.col_name can be used


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...