2013-12-21 75 views
0

使用MySQL 5.5,以下觸發被拒一個錯誤: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 '-category for each row begin if(catid >= relatedid) then signal 'catid must be l' at line 1

什麼是錯的這個觸發器的語法?

對於它的價值,我只是想阻止插入與catid >= relatedid。我願意接受實現這一目標的其他方法。


編輯: 上述被在一行上,分號和所有輸入的查詢。

我用分隔符重新嘗試了一遍。這裏是結果:

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 

回答

5

-是一個特殊字符。您需要使用反向引號來跳過包含特殊字符的表格

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 

您還需要更改分隔符。否則數據庫認爲你的觸發器定義在第一個;結束,這將是不完整的。

+0

謝謝,我改變了它。仍然收到相同的錯誤 –

+0

實際上,錯誤稍有不同:'錯誤1064(42000):您的SQL語法錯誤;檢查與您的MySQL服務器版本相對應的手冊,以便在第1行'end if end'附近使用正確的語法。 –

+0

我在問題中添加了一個編輯...觸發器正在MySQL終端的單行中輸入提示 –

0

最明顯的問題是category-category不是有效的標識符。將其列入反引號:

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