Add constraint on values , Mysql

≡放荡痞女 提交于 2019-12-02 04:43:25

From CREATE TABLE:

The CHECK clause is parsed but ignored by all storage engines.

Second:

CREATE TRIGGER SexCheck BEFORE INSERT ON  STUDENT
FOR EACH ROW
BEGIN
    IF New.Sex NOT IN('F', 'M') THEN
    SIGNAL SQLSTATE '10000'
        SET MESSAGE_TEXT = 'check constraint on Student.Sex failed';
    END IF;
END;


INSERT INTO STUDENT(Sex) VALUES ('B');
-- check constraint on Student.Sex failed

SqlFiddleDemo

Use the MySQL ENUM type. "An ENUM is a string object with a value chosen from a list of permitted values that are enumerated explicitly in the column specification at table creation time."

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