简体   繁体   English

MySQL触发器,语法模糊

[英]MySQL Trigger, vague syntax error

Using MySQL 5.5, the following trigger is getting rejected with an 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;

I'm getting the following error: 我收到以下错误:

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

What is wrong with the syntax for this trigger? 此触发器的语法有什么问题?

For what it's worth, I'm just trying to prevent inserts with catid >= relatedid . 对于它的价值,我只是想防止使用catid >= relatedid插入。 I am open to other methods of achieving this goal. 我对实现这一目标的其他方法持开放态度。


Edit: The query above is being entered on a single line, semi-colons and all. 编辑:上面的查询正在单行,分号和所有上输入。

I tried it again using a delimiter. 我再次尝试使用定界符。 Here is the result: 结果如下:

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

- is a special character. -是一个特殊字符。 You need to escape a table containg special characters with backticks 您需要转义包含带有反引号的特殊字符的表

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 

You also need to change the delimiter. 您还需要更改定界符。 Otherwise the DB thinks your trigger definition ends at the first ; 否则,数据库会认为您的触发器定义在第一个结尾处结束; which would be incomplete. 这将是不完整的。

The most obvious problem is that category-category is not a valid identifier. 最明显的问题是category-category不是有效的标识符。 Enclose it in backquotes: 将其括在反引号中:

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

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

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