Reputation: 1255
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
Upvotes: 0
Views: 1147
Reputation: 204766
-
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.
Upvotes: 5
Reputation: 1269873
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`
. . .
Upvotes: 0