创建 MySQL 触发器的 SQL 语法错误
我尝试创建触发器
CREATE TRIGGER `aster_users2` after
update ON `aster_users` FOR EACH ROW
BEGIN update event set flag=1 where
id=1; END;
但出现下一个错误
错误 1064 (42000):您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 6 行附近“结束”处使用的正确语法
是否有解决此问题的建议?
I try create trigger
CREATE TRIGGER `aster_users2` after
update ON `aster_users` FOR EACH ROW
BEGIN update event set flag=1 where
id=1; END;
but got next error
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'end' at line 6
have suggestion to solve this problem ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试从语句中删除分号。
如果您想保留分号,
Try removing the semi-colons from your statements.
If you'd like to keep your semi-colons,
您可以:
删除
BEGIN
和END
(仅当正文中有单个语句时才可能):或
添加DELIMITER说明符整个
CREATE TRIGGER
语句:<前><代码>分隔符|
之后创建触发器“aster_users2”
更新每一行的 `aster_users`
开始
更新事件集标志 = 1,其中 id = 1;
结束|
分隔符;
请注意第二个
DELIMITER
,它会恢复默认语句分隔符。编辑 – 说明:
通常您使用
;
来分隔语句。但是,当涉及像CREATE TRIGGER
这样使用BEGIN/END
并允许您在其主体中包含多个语句的复合语句时,解析器需要一种方法来区分分隔符整个复合语句之后的正文语句之间的分隔符。因此,您需要以某种方式避免在复合语句中使用
;
或告诉解析器复合语句将使用不同的分隔符。如果您只需在END
之前删除;
也可以实现第一个选项,例如 @p.campbell 已建议。You can either:
drop
BEGIN
andEND
(is only possible when there's a single statement in the body):or
add the DELIMITER specifier for the entire
CREATE TRIGGER
statement:Note the second
DELIMITER
, which restores the default statement delimiter.EDIT – Explanation:
Generally you are using
;
to delimit statements. But when it comes to compound statements likeCREATE TRIGGER
, which useBEGIN/END
and allow you to include multiple statements in their bodies, the parser needs a way to distinguish the delimiters between the body's statements from the delimiter after the entire compound statement.Thus you need to either refrain somehow from using
;
inside the compound statement or tell the parser that the compound statement will use a different delimiter. The first option can also be achieved if you just drop;
beforeEND
, like @p.campbell has suggested.应使用分隔符。
Delimiters should be used.