Mysql 5.7.20触发错误

时间:2018-03-26 10:20:36

标签: mysql sql

我必须在我的学校表中列出学校表历史记录表,我有一个status_id列,所以我想要的是每次status_id列都会更新我想将 status_id school_id 发布到历史记录表

这就是我的尝试:

DELIMITER ;;
  CREATE trigger homestead.statustrigger 
  AFTER UPDATE ON homestead.schools
  FOR EACH ROW
  BEGIN
    IF NOT NEW.status_id <=> OLD.status_id
    INSERT INTO histories (status_id, schools_id)
    VALUES (1, 1);
  END
DELIMITER;

当我运行此查询时,我收到此错误:

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 'INSERT INTO histories (status_id, school_id)
    VALUES (1, 1);
END
DELIMITER' at line 6

3 个答案:

答案 0 :(得分:0)

你的If条件在语法上是不正确的。 当您使用Insert语句时,请使用Before Insert

  CREATE trigger homestead.statustrigger 
  BEFORE INSERT ON homestead.schools
  FOR EACH ROW
  BEGIN
    IF NOT NEW.status_id <=> OLD.status_id Then
    INSERT INTO histories (status_id, churches_id)
    VALUES (1, 1);
    END IF;
  END;

答案 1 :(得分:0)

IF语法错误,您声明了DELIMITER然后您不使用,这应该有效:

DELIMITER $$

DROP TRIGGER IF EXISTS homestead.statustrigger  $$

CREATE trigger homestead.statustrigger 
AFTER UPDATE ON homestead.schools
FOR EACH ROW
BEGIN
  IF NOT NEW.status_id <=> OLD.status_id THEN
    INSERT INTO histories (status_id, churches_id)
    VALUES (1, 1);
  END IF;
END $$

DELIMITER ;

答案 2 :(得分:0)

您将DELIMITER设置为;;。因此,请删除DELIMITER ;;或在触发器定义的末尾添加;;,如

CREATE trigger homestead.statustrigger
   AFTER UPDATE ON homestead.schools
   FOR EACH ROW
BEGIN
  IF NOT NEW.status_id <=> OLD.status_id THEN
    INSERT INTO histories (status_id, churches_id)
    VALUES (1, 1);
  END IF;
END;;