0

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

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

3 Comments

Actually, the error is slightly different: 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 'end if end' at line 1
I added an edit to the question...the trigger is being entered in a single line at the MySQL terminal prompt
Got it...was also missing the semi-colon after end if. Thank you very much.
0

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`
. . .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.