简体   繁体   中英

MySQL Trigger, vague syntax error

Using MySQL 5.5, the following trigger is getting rejected with an error:

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

What is wrong with the syntax for this trigger?

For what it's worth, I'm just trying to prevent inserts with 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. Enclose it in backquotes:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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