开发者

MySQL Trigger issues

So I am trying to set up a trigger that when a new row to my user table is created a new 开发者_开发知识库row is also created in my profile table and the primary key in the user table is inserted into the foreign key slot in the profile table. This is the code I am using at the moment and it hasn't been working out.

    CREATE TRIGGER userID
AFTER INSERT ON user FOR EACH ROW
BEGIN
    INSERT INTO profile (userID)
    VALUES(user.userID);
END

The foreign key on the profile table and the primary on the user table are both called userID


Saying user.userID doesn't mean anything useful in that context, you want to reference the newly created row using NEW:

CREATE TRIGGER userID
AFTER INSERT ON user FOR EACH ROW
BEGIN
    INSERT INTO profile (userID)
    VALUES(NEW.userID);
END

From the fine manual:

You can refer to columns in the subject table (the table associated with the trigger) by using the aliases OLD and NEW. [...] NEW.col_name refers to the column of a new row to be inserted or an existing row after it is updated.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜