MySQL Trigger"语法错误,意外END,期待&#39 ;;'"

时间:2015-01-29 13:24:17

标签: mysql triggers

我试图创建一个触发器来验证客户表中的电子邮件地址,但我收到了错误

  

语法错误,意外END,期待&#39 ;;'

我的代码如下:

CREATE TRIGGER insert_validation_email
 BEFORE INSERT ON customer FOR EACH ROW
    BEGIN
        IF (NEW.CustomerEmail NOT REGEXP '^[^@]+@[^@]+\.[^@]{2,}$')
        THEN
            SIGNAL SQLSTATE '45000'
            SET MESSAGE_TEXT = 'Cannot add or update row: invalid email address'
    END* IF
END

END *是带有错误的下划线(星号不存在)

还会包含其他验证触发器,因此需要使用begin和end语句

1 个答案:

答案 0 :(得分:0)

通过更改分隔符和放置来修复; if if block和final END之后。感谢Gordon Linoff和a_horse_with_no_name帮助

DELIMITER $$
CREATE TRIGGER before_insert_email
     BEFORE INSERT ON customer FOR EACH ROW
        BEGIN
            IF (NEW.CustomerEmail NOT REGEXP '^[^@]+@[^@]+\.[^@]{2,}$')
            THEN
                SIGNAL SQLSTATE '45000'
                SET MESSAGE_TEXT = 'Cannot add or update row: invalid email address';
        END IF;
    END$$
DELIMITER ;