如何使用触发器检查复合列上的INSERT操作?

时间:2015-01-23 20:32:50

标签: mysql sql triggers

我有下一个表:教师,教师,主题和科目。每个教师都有一个主题列表

db schema

我想使用checkthe faculty_subjects表格 - 每个教师不应该多次拥有相同的主题。因此,faculty_subjects表格中的每条记录都有一对唯一的Faculty_idFaculty - Subject_idSubject

我正在尝试添加下面列出的触发器:

DELIMITER $$
CREATE TRIGGER `Faculty_Subjects_BINS` BEFORE INSERT ON `Faculty_Subjects` FOR EACH ROW
BEGIN
    -- throw exception when condition not satisfied
    IF ( (NEW.`Subject_idSubject`= `Subject_idSubject`) AND (NEW.`Faculty_idFaculty`= `Faculty_idFaculty`)) THEN
        SIGNAL SQLSTATE '45000'
            SET MESSAGE_TEXT = 'This faculty already add this subject as preliminary', MYSQL_ERRNO = 1001;
    END IF;
END;

但我收到错误11091054。如何实现这一功能?

我正在使用MySQL数据库。

0 个答案:

没有答案