因此,我尝试设置一个触发器,当我的用户表创建新行时,我的配置文件表中也会创建一个新行,并且用户表中的主键将插入到概况表。这是我目前正在使用的代码,但尚未成功。
CREATE TRIGGER userID
AFTER INSERT ON user FOR EACH ROW
BEGIN
INSERT INTO profile (userID)
VALUES(user.userID);
END
配置文件表上的外键和用户表上的主键都称为 userID
最佳答案
说user.userID
在该上下文中并不意味着任何有用的东西,您想使用NEW
引用新创建的行:
CREATE TRIGGER userID
AFTER INSERT ON user FOR EACH ROW
BEGIN
INSERT INTO profile (userID)
VALUES(NEW.userID);
END
来自fine manual :
You can refer to columns in the subject table (the table associated with the trigger) by using the aliases
OLD
andNEW
. [...]NEW.col_name
refers to the column of a new row to be inserted or an existing row after it is updated.
关于MySQL 触发器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6811494/