簡體   English   中英

MySQL觸發器,語法模糊

[英]MySQL Trigger, vague syntax error

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

我收到以下錯誤:

錯誤1064(42000):您的SQL語法有錯誤; 檢查與您的MySQL服務器版本相對應的手冊以獲取正確的語法,以在'-category每行開始時使用if(catid> = relatedid),然后在第1行信號'catid必須為l'

此觸發器的語法有什么問題?

對於它的價值,我只是想防止使用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

-是一個特殊字符。 您需要轉義包含帶有反引號的特殊字符的表

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 

您還需要更改定界符。 否則,數據庫會認為您的觸發器定義在第一個結尾處結束; 這將是不完整的。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM