在IF / THEN上的MySQL中的触发语法错误(错误1064)

时间:2012-10-16 03:31:00

标签: mysql if-statement

执行此查询时出错:

CREATE TRIGGER insert_Topics
  BEFORE INSERT
  ON Topics
  FOR EACH ROW
  BEGIN
    IF (SELECT COUNT(*) FROM Subjects WHERE ID=new.SubjectID)=0
    THEN
      INSERT error_msg VALUES ('Foreign Key Constraint Violated!');
END IF;
END;
delimiter ;

错误说:

  

1064 - 您的SQL语法出错;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   靠近''第8行

并指向单词THEN

有任何帮助吗?提前谢谢。

2 个答案:

答案 0 :(得分:1)

两个

CREATE TRIGGER insert_Topics
BEFORE INSERT
ON Topics
FOR EACH ROW
BEGIN
  IF NOT EXISTS(SELECT 1 FROM Subjects WHERE ID=NEW.SubjectID LIMIT 1) THEN
    INSERT INTO error_msg VALUES ('Foreign Key Constraint Violated!');
  END IF;
END;

CREATE TRIGGER insert_Topics
BEFORE INSERT
ON Topics
FOR EACH ROW
BEGIN
  IF (SELECT 1 FROM Subjects WHERE ID=NEW.SubjectID LIMIT 1) IS NULL THEN
    INSERT INTO error_msg VALUES ('Foreign Key Constraint Violated!');
  END IF;
END;

为我工作,所以你的语法问题可能在其他地方。

答案 1 :(得分:1)

我知道这是一个老问题,但我想我会分享我对我认为错误的想法,以防万一有人在将来看到这个问题(如果我有这样的话,请随时进行社区编辑。我犯了任何错误!)。

原始问题中存在问题的原因似乎是使用DELIMITER及其功能,如下所述:What does DELIMITER // do in a Trigger?

在给定的代码中:

        INSERT error_msg VALUES ('Foreign Key Constraint Violated!');
END IF;
END;
delimiter ;

使用"分隔符重置分隔符;"表示它先前已更改为另一个分隔符,例如:

DELIMITER //
--TRIGGER1
--TRIGGER2
--TRIGGERn
delimiter ; -- Aggravates me that this wasn't capitalised :P

将分隔符设置为" //" (或其他),是允许分隔多个语句,我想的是,原始作者在触发器之上有其他触发器,并且已将分隔符更改为类似" //& #34;,但忘了改变:

END IF;
END;
delimiter ;

要:

END IF;
END//
delimiter ;

因此,评论者无法复制的原因是因为他们有正确的分隔符集(&#34 ;;"),并且因为只有一个语句要分隔,所以&# 39;找不到任何错误,正如作者所做的那样。 请参阅以下测试代码和结果,包括初始化,作为一些证据:

DELIMITER ; -- Just in case
DROP TABLE IF EXISTS Topics, Subjects, error_msg;

CREATE TABLE Subjects ( id INT(5) PRIMARY KEY,
                        subjectname CHAR(20));
CREATE TABLE Topics ( topicname CHAR(20),
                      subjectID INT(5)
                    # FOREIGN KEY (subjectID) REFERENCES Subjects(id) - How this check should be done...
                    );
CREATE TABLE error_msg ( Message VARCHAR(50) );

INSERT INTO Subjects VALUES
    ('5', 'Arts'),
    ('55', 'Maths'),
    ('2342', 'Biology'),
    ('12345', 'Finance');

DELIMITER $$ -- Where the delimiter would be defined, originally 
    CREATE TRIGGER InsertOnTopics
        BEFORE INSERT ON Topics
        FOR EACH ROW
        BEGIN
            IF (SELECT COUNT(*) FROM Subjects WHERE id=NEW.subjectID)=0 THEN
                INSERT error_msg VALUES ('Foreign Key Constraint Violated!');
            END IF;
        END$$
DELIMITER ;

INSERT INTO Topics VALUES
    ('Welfare Benefits', '5'),
    ('Eigenvectors', '55'),
    ('Mitochondria', '2342'),
    ('Bank of Dad', '12345'),
    ('Something else', '555');

SELECT * FROM error_msg;

使用"结束;",作为原始代码:

mysql> SOURCE /Users/benpalmer/test.sql
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.02 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.02 sec)

Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.02 sec)

ERROR 1064 (42000): 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 '('Something else', '555')' at line 6
Empty set (0.00 sec)

mysql>

使用" END $$",在我的代码中:

mysql> SOURCE /Users/myUsername/test.sql
Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.02 sec)

Query OK, 0 rows affected (0.02 sec)

Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.02 sec)

Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0

+----------------------------------+
| Message                          |
+----------------------------------+
| Foreign Key Constraint Violated! |
+----------------------------------+
1 row in set (0.00 sec)

mysql>

除此之外,我不明白为什么这个特定的例子没有使用适当的外键约束。作为我的第一个答案...希望这有助于任何人!也坚持了一段时间。