我正在尝试在MySQL
中实现一个触发器,该触发器会在添加/修改另一个列时自动添加/修改列。基本上,我希望在type
中添加/修改值时,type_sanitized
type
作为slug保存。
这是我最初的尝试:
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小时的搜索中找到它,所以任何帮助都将非常感激。
答案 0 :(得分:1)
删除撇号。
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 @type_sanitized = LOWER(REPLACE(TRIM(@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 @type_sanitized = LOWER(REPLACE(TRIM(@type), ' ', '-')); END$$ DELIMITER ;