由于错误,将不会创建MYSQL触发器

时间:2013-04-06 14:34:50

标签: mysql triggers phpmyadmin

我正在尝试在MYSQL 5.0.95中触发,以防止在电子邮件字段中找不到@符号时发生插入。我知道除了触发器之外还有其他方法可以做到这一点,但我真的想了解触发器是如何工作的

CREATE TRIGGER triggerUsersEmail 
BEFORE INSERT ON Users 
FOR EACH ROW 
BEGIN 
IF (Users.email NOT LIKE '%@%') THEN
DECLARE dummy INT;
SELECT not a valid email address INTO dummy FROM Users WHERE 1=1
END IF;
END;$$ 

我在PHPMYADMIN中使用$$的分隔符运行上面的内容,但是我收到此错误:

#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 'DECLARE dummy INT; SELECT not a valid email address INTO dummy FROM Users WHERE' at line 6

1 个答案:

答案 0 :(得分:0)

你试过这个吗?


    CREATE TRIGGER triggerUsersEmail 
    BEFORE INSERT ON Users 
    FOR EACH ROW 
    BEGIN 
    DECLARE dummy INT;
    IF (Users.email NOT LIKE '%@%') THEN
    SELECT not a valid email address INTO dummy FROM Users WHERE 1=1
    END IF;
    END;$$ 

开始后声明变量。

_ _ 编辑

DELIMITER $$
CREATE TRIGGER triggerUsersEmail 
BEFORE INSERT ON users 
FOR EACH ROW 
BEGIN 
    DECLARE dummy INT;

    IF (Users.email NOT LIKE '%@%') THEN
        SELECT "not a valid email address" INTO dummy FROM Users WHERE 1=1;
    END IF;
END;

$$ 

这可以在我的服务器上运行