MySQL触发器:阻止IF语句插入

时间:2016-09-25 19:59:32

标签: mysql if-statement triggers syntax-error

我尝试编写MySQL数据库的第一个触发器。在当前时间的上下文中阻止插入(以及稍后还会更新)“ course_student ”以及“容量”中的给定时间戳,但我仍然遇到语法错误。

DELIMITER $$

CREATE TRIGGER checkSubscribeTimeCourse 
BEFORE INSERT ON course_student
REFERENCING NEW AS new
FOR EACH ROW 

BEGIN

    IF (SELECT COUNT(*) FROM capacity c, course_capacity cc 
        WHERE c.cid = cc.cid 
        AND cc.cid = new.cid
        AND (c.end >= CURRENT_TIMESTAMP OR c.end IS NULL)
        AND (c.start <= CURRENT_TIMESTAMP OR c.start IS NULL)<1)
    THEN
        SIGNAL SQLSTATE '45000'
        SET MESSAGE_TEXT = 'Error'
    END IF

END

DELIMITER ;

我翻译了所有名字;所以可能有打字错误。

1 个答案:

答案 0 :(得分:2)

你有一些缺少的逗号,有些编造了sql,最后还有一个缺失的分隔符。

DELIMITER $$

CREATE TRIGGER checkSubscribeTimeCourse 
BEFORE INSERT ON course_student
FOR EACH ROW
BEGIN

    IF (SELECT COUNT(*) FROM capacity c, course_capacity cc 
        WHERE c.cid = cc.cid 
        AND cc.cid = new.cid
        AND (c.end >= CURRENT_TIMESTAMP OR c.end IS NULL)
        AND (c.start <= CURRENT_TIMESTAMP OR c.start IS NULL)<1)
    THEN
        SIGNAL SQLSTATE '45000'
        SET MESSAGE_TEXT = 'Error';
    END IF;

END$$

DELIMITER ;