首页 文章

MySQL Trigger,模糊的语法错误

提问于
浏览
0

使用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;

我收到以下错误:

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

What is wrong with the syntax for this trigger?

为什么's worth, I'只是试图阻止插入 catid >= relatedid . 我对实现这一目标的其他方法持开放态度 .


Edit: 上面的查询输入单行,分号和全部 .

我用分隔符再次尝试了 . 结果如下:

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

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

    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
    

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

  • 5

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

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

相关问题