MySQL Trigger,模糊的语法错误

时间:2013-12-21 17:06:10

标签: 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;

我收到以下错误:

  

错误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

2 个答案:

答案 0 :(得分: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 

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

答案 1 :(得分:0)

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

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