mysql奇怪的触发错误

时间:2013-11-26 06:43:04

标签: mysql triggers

我正在尝试设置一个触发器(从单独的.sql文件运行)以防止像这样插入:

create trigger insert_formulas before insert on formulas    
for each row
    if ((new.formula_id_1 is not null) and (new.semantic_id_1 is not null)) 
    or ((new.formula_id_2 is not null) and (new.semantic_id_2 is not null))
    then
        signal sqlstate '45100';
        set @message_text = 'A formula cannot be a definition at the same time';
    end if;

它在第6行附近产生错误1064,在第1行附近产生错误1064.我做错了什么?

提前致谢。

1 个答案:

答案 0 :(得分:0)

9.4. User-Defined Variables @message_text13.6.7.5. SIGNAL Syntax定义中的MESSAGE_TEXT不同吗?

以下代码正确创建了触发器:

DELIMITER $$

CREATE TRIGGER `insert_formulas` BEFORE INSERT ON `formulas`
FOR EACH ROW
BEGIN
    IF((NEW.`formula_id_1` IS NOT NULL AND NEW.`semantic_id_1` IS NOT NULL) OR
        (NEW.`formula_id_2` IS NOT NULL AND NEW.`semantic_id_2` IS NOT NULL)) THEN
        SIGNAL SQLSTATE '45100'
        SET MESSAGE_TEXT = 'A formula cannot be a definition at the same time';
    END IF;
END$$

DELIMITER ;

此处SQL Fiddle