MySQL Trigger,模糊的语法错误

Dav*_*ski 0 mysql sql triggers

使用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;
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

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

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

对于它的价值,我只是想阻止插入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
Run Code Online (Sandbox Code Playgroud)

jue*_*n d 5

-是一个特殊的角色.你需要使用反引号来逃避包含特殊字符的表格

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 
Run Code Online (Sandbox Code Playgroud)

您还需要更改分隔符.否则,DB认为您的触发器定义在第一个结束时结束,;这将是不完整的.