同一 table 中的两列必须匹配 - Mysql

Two columns in the same table must match - Mysql

如何添加约束以强制同一 table 中两列的值相同?

我希望学生 class 的成绩与学生的成绩一致。

/*Student Grade = Student Class Grade constraint "BEFORE INSERT" */ 
delimiter //    
CREATE TRIGGER StCGradeCheckBIN BEFORE INSERT ON  STUDENT
FOR EACH ROW
BEGIN
    IF new.Class_Grade <> Grade THEN
    SIGNAL SQLSTATE '10000'
        SET MESSAGE_TEXT = 'Error while filling the Class Grade faild! It is must match the Student.Grade feild';
    END IF;
END;
// delimiter ;


/*Student Grade = Student Class Grade constraint "BEFORE UPDATE" */ 

delimiter //    
CREATE TRIGGER StCGCheckBUP BEFORE UPDATE ON  Student
FOR EACH ROW
BEGIN
    IF new.Class_Grade <> Grade THEN
    SIGNAL SQLSTATE '10000'
        SET MESSAGE_TEXT = 'Error while Updating the Class Grade feild! It is must mathc  the Student.Grade feild';
    END IF;
END;
// delimiter ;

这个触发器不起作用,它在插入 STUDENT 期间导致这个错误 table

Error Code: 1054. Unknown column 'Grade' in 'field list'

使用 mysql workbench 6.3

提前致谢

还不能发表评论,但如果两列必须相等,为什么还需要第二列?它不会有任何作用。

问题是触发器不理解 Grade。因为它们在同一个 table 上,我认为你可以只使用 new. 来限定名称:

delimiter //    
CREATE TRIGGER StCGradeCheckBIN BEFORE INSERT ON  STUDENT
FOR EACH ROW
BEGIN
    IF new.Class_Grade <> new.Grade THEN
--------------------------^
    SIGNAL SQLSTATE '10000'
        SET MESSAGE_TEXT = 'Error while filling the Class Grade faild! It is must match the Student.Grade feild';
    END IF;
END;
// delimiter ;