[英]mysql Trigger On insert
whats wrong with my syntax?我的语法有什么问题?
CREATE
TRIGGER db_dhruniversity.trigger1
AFTER INSERT
ON jos_dhruprofile
FOR EACH ROW
BEGIN
UPDATE jos_users
SET jos_users.department = jos_dhruprofile.department
WHERE jos_users.id = jos_dhruprofile.uid
END
The syntax should be as follows:语法应如下所示:
DELIMITER $$ /* if you're not using an editor, you must change the delimiter*/
CREATE
TRIGGER ai_jos_dhruprofile_each
AFTER INSERT
ON jos_dhruprofile
FOR EACH ROW
BEGIN
UPDATE jos_users
SET jos_users.department = NEW.department
WHERE jos_users.id = NEW.uid; /*<<<--- ; after every stament */
END $$ /* changed delimiter after the end */
DELIMITER ; /*make sure you set the delimiter back to the default*/
Note on the naming scheme for triggers关于触发器命名方案的注意事项
I'd recommend naming your trigger ai
(meaning after insert
) so you know when it fires on which table, rather than a meaningless name like: db_dhruniversity.trigger1
.我建议将您的触发器命名为ai
(意思是after insert
),这样您就知道它何时在哪个表上触发,而不是一个无意义的名称,例如: db_dhruniversity.trigger1
。
I always use [a/b]+[d/i/u]_tablename_each as the triggername, that way I always know when the triggers fires (before/after) for which event (insert/delete/update) and on which table.我总是使用[a/b]+[d/i/u]_tablename_each作为触发器名称,这样我总是知道触发器何时触发(之前/之后)哪个事件(插入/删除/更新)以及哪个表。
It's also good practise to document that the trigger fires on each
row, hence the each
on the end of the trigger name.记录触发器在each
行上触发也是一个好习惯,因此each
触发器名称的末尾都有。
Note that MySQL does not support triggers that fire once per statement yet (But that might change in future).请注意,MySQL还不支持每个语句触发一次的触发器(但将来可能会改变)。
There are no delimiters in it:其中没有分隔符:
DELIMITER ||
CREATE
TRIGGER db_dhruniversity.trigger1
AFTER INSERT
ON jos_dhruprofile
FOR EACH ROW
BEGIN
UPDATE jos_users
SET jos_users.department = NEW.department
WHERE jos_users.id = NEW.uid;
END ||
DELIMITER;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.