我试图写一个mysql触发器,它只允许2列中的1列有一个值。该表如下所示:
tbl_lesson
lessonID int NOT NULL AUTO_INCREMENT ==> PRIMARY KEY
studentID int ==> FOREIGN KEY
classID int ==> FOREIGN KEY
我的触发器如下所示:
DELIMITER $$
CREATE TRIGGER test_before_insert BEFORE INSERT ON tbl_lesson
FOR EACH ROW
BEGIN
declare counter int DEFAULT 0;
IF studentID IS NULL AND classID IS NOT NULL THEN
set counter := counter + 1;
END IF;
IF studentID IS NOT NULL AND classID IS NULL THEN
set counter := counter + 1;
END IF;
IF counter != 1 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEST = "Only studentID or classID must be null";
END IF;
END$$
DELIMITER ;
目前,每当我尝试向该表输入任何内容时(无论它是否有效),都会返回此错误。
ERROR 1054 (42S22): Unknown column 'studentID' in 'field list'
非常感谢您的帮助。