Mysql创建触发器抛出错误

时间:2015-08-30 13:40:34

标签: mysql database triggers insert

我试图为每个新帖子创建触发器 这是我的代码

CREATE TRIGGER insertprod AFTER INSERT ON post
FOR EACH ROW
BEGIN
  INSERT INTO attributes SET attrtext = 'sometext', post_id = NEW.id;
END;

我收到此错误

enter image description here

3 个答案:

答案 0 :(得分:2)

您可能只需要一个分隔符:

DELIMITER $$
CREATE TRIGGER insertprod AFTER INSERT ON post
FOR EACH ROW
BEGIN
  INSERT INTO attributes SET attrtext = 'sometext', post_id = NEW.id;
END;
$$
DELIMITER ;

但是,您也使用非标准形式的INSERT。我建议:

DELIMITER $$
CREATE TRIGGER insertprod AFTER INSERT ON post
FOR EACH ROW
BEGIN
  INSERT INTO attributes(attrtext, post_id)
     VALUES('sometext', NEW.id);
END;
$$
DELIMITER ;

答案 1 :(得分:0)

您的INSERT命令错误。应该是

 INSERT INTO attributes (attrtext, post_id) VALUES ('sometext', NEW.id);

答案 2 :(得分:0)

解析器被;混淆,BEGIN...END终止了触发器DELIMITER块内的语句。您需要使用CREATE TRIGGER语句来定义新的分隔符,然后可以使用该分隔符结束class MyContaier { private List<MyObject> m_listOfObjects; public MyContainer() { ... } public void addObject(MyObject object) { m_listOfObjects.add(object); } public int calculateTotal() { int total = 0; for (MyObject object : m_listOfObjects) total += object.getValue(); return total; } } 语句。