MySql workbench CHECK constraint [duplicate]

戏子无情 提交于 2019-12-04 08:24:28

Since MySQL does not support check, you need a trigger for that. Something like this CREATE trigger:

delimiter $$
CREATE TRIGGER some_trigger_name
BEFORE INSERT ON SubjectEnrollment
FOR EACH ROW
BEGIN    
    IF (NEW.register > NEW.classSize OR NEW.register < 0)        
        SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'invalid data';
    END IF;
END
$$

You need to define the same trigger for UPDATEs.

A bit long for a comment.

MySQL supports the syntax for check, but in the create table:

create table (
    . . . 
    CHECK (register <= classSize AND register >= 0)
);

Check is not actually implemented in any engines, so this does nothing. Presumably for that reason, it is not part of the constraint syntax and hence not available for add constraint.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!