gpt4 book ai didi

MySQL 触发器语法错误 #1064

转载 作者:行者123 更新时间:2023-11-29 13:08:18 27 4
gpt4 key购买 nike

我正在尝试在 MySQL 中实现一个触发器,当添加/修改另一列时,该触发器将自动添加/修改一列。基本上我希望它将 type 保存为 slug当将值添加/修改为 type 时,在 type_sanitized 列中。

这是我最初的尝试:

DROP TRIGGER IF EXISTS ArticleTypes.insert_sanitized_article_type;

CREATE TRIGGER 'insert_sanitized_article_type'
AFTER INSERT
ON 'ArticleTypes'
FOR EACH ROW
SET NEW.type_sanitized = LOWER(REPLACE(TRIM(NEW.type), ' ', '-'));

DROP TRIGGER IF EXISTS ArticleTypes.update_sanitized_article_type;

CREATE TRIGGER 'update_sanitized_article_type'
AFTER UPDATE
ON 'ArticleTypes'
FOR EACH ROW
SET NEW.type_sanitized = LOWER(REPLACE(TRIM(NEW.type), ' ', '-'));

在网上看到建议后,我尝试添加这样的分隔符:

DROP TRIGGER IF EXISTS ArticleTypes.insert_sanitized_article_type;

DELIMITER $$
CREATE TRIGGER 'insert_sanitized_article_type'
AFTER INSERT
ON 'ArticleTypes'
FOR EACH ROW
BEGIN
SET NEW.type_sanitized = LOWER(REPLACE(TRIM(NEW.type), ' ', '-'));
END$$
DELIMITER ;

DROP TRIGGER IF EXISTS ArticleTypes.update_sanitized_article_type;

DELIMITER $$
CREATE TRIGGER 'update_sanitized_article_type'
AFTER UPDATE
ON 'ArticleTypes'
FOR EACH ROW
BEGIN
SET NEW.type_sanitized = LOWER(REPLACE(TRIM(NEW.type), ' ', '-'));
END$$
DELIMITER ;

我收到以下错误:

#1064 - 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 ''insert_sanitized_article_title'
AFTER INSERT
ON 'ArticleTypes'
FOR EACH ROW
BE' at line 1

这是我第一次在 MySQL 中创建触发器,所以它可能是显而易见的,但我在 2 小时的搜索中还没有弄清楚,所以任何帮助将不胜感激。

最佳答案

删除撇号。

DROP TRIGGER IF EXISTS ArticleTypes.insert_sanitized_article_type;DELIMITER $$CREATE TRIGGER insert_sanitized_article_typeAFTER INSERTON ArticleTypesFOR EACH ROW BEGINSET @type_sanitized = LOWER(REPLACE(TRIM(@type), ' ', '-'));END$$DELIMITER ;DROP TRIGGER IF EXISTS ArticleTypes.update_sanitized_article_type;DELIMITER $$CREATE TRIGGER update_sanitized_article_typeAFTER UPDATEON ArticleTypesFOR EACH ROW BEGINSET @type_sanitized = LOWER(REPLACE(TRIM(@type), ' ', '-'));END$$DELIMITER ;

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

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