2013-12-21 52 views
0

使用MySQL 5.5,以下触发被拒一个错误: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 '-category for each row begin if(catid >= relatedid) then signal 'catid must be l' at line 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 

回答

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 

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

+0

谢谢,我改变了它。仍然收到相同的错误 –

+0

实际上,错误稍有不同:'错误1064(42000):您的SQL语法错误;检查与您的MySQL服务器版本相对应的手册,以便在第1行'end if end'附近使用正确的语法。 –

+0

我在问题中添加了一个编辑...触发器正在MySQL终端的单行中输入提示 –

0

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

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