gpt4 book ai didi

MySQL 触发器,模糊的语法错误

转载 作者:行者123 更新时间:2023-12-01 00:47:09 28 4
gpt4 key购买 nike

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

我收到以下错误:

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

这个触发器的语法有什么问题?

就其值(value)而言,我只是试图阻止使用 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

您还需要更改分隔符。否则 DB 认为您的触发器定义在第一个 ; 结束,这将是不完整的。

关于MySQL 触发器,模糊的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20721409/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com