Add constraint on values , Mysql

后端 未结 2 641
南笙
南笙 2021-01-26 00:01

My table

CREATE TABLE STUDENT ( 
      BCN INT not null,
      Stname varchar(50) not null,
      Sex char(1) not null ,
      primary key (BCN));
2条回答
  •  迷失自我
    2021-01-26 00:34

    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

提交回复
热议问题