繁体   English   中英

MySQL触发器,语法模糊

[英]MySQL Trigger, vague syntax error

使用MySQL 5.5,以下触发器被拒绝并显示错误:

create trigger nodups
before insert on `category-category`
for each row
begin
    if(catid >= relatedid) then
        signal 'catid must be less than relatedid';
    end if
end;

我收到以下错误:

错误1064(42000):您的SQL语法有错误; 检查与您的MySQL服务器版本相对应的手册以获取正确的语法,以在'-category每行开始时使用if(catid> = relatedid),然后在第1行信号'catid必须为l'

此触发器的语法有什么问题?

对于它的价值,我只是想防止使用catid >= relatedid插入。 我对实现这一目标的其他方法持开放态度。


编辑:上面的查询正在单行,分号和所有上输入。

我再次尝试使用定界符。 结果如下:

mysql> delimiter ###
mysql> create trigger nodups
    -> before insert on `category-category`
    -> for each row
    -> begin
    -> if(catid >= relatedid) then   
    -> signal 'catid must be less than relatedid';
    -> end if
    -> end;
    -> ###
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 ''catid must be less than relatedid';
end if
end' at line 6

-是一个特殊字符。 您需要转义包含带有反引号的特殊字符的表

delimiter |
create trigger nodups
before insert on `category-category`
for each row
begin
    if(catid >= relatedid) then
        SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'catid must be less than relatedid';
    end if;
end;
|
delimiter 

您还需要更改定界符。 否则,数据库会认为您的触发器定义在第一个结尾处结束; 这将是不完整的。

最明显的问题是category-category不是有效的标识符。 将其括在反引号中:

create trigger nodups
before insert on `category-category`
. . .

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM