在值上添加约束,Mysql

时间:2015-11-15 22:09:21

标签: mysql sql mysql-workbench workbench

我的表

CREATE TABLE STUDENT ( 
      BCN INT not null,
      Stname varchar(50) not null,
      Sex char(1) not null ,
      primary key (BCN));

我想让性别属性只接受两个价值观' F'或者' M'

我试过检查'条款:

 Sex char(1) not null check (Sex = 'F' or Sex ='M' ) 

但不起作用,Sex列仍然接受其他值,例如(' B')!

我尝试创建触发器:

DELIMITER $$ CREATE TRIGGER SexCheck BEFORE INSERT ON student
FOR EACH ROW
BEGIN
    IF Sex <> 'M' and Sex <> 'F' THEN
    SIGNAL SQLSTATE '10000'
        SET MESSAGE_TEXT = 'check constraint on Student.Sex failed';
    END IF;
END$$   
DELIMITER ;

它不会产生任何语法错误,但Sex列不接受任何值,即使M&#39; M&#39; M&#39; M&#39;或者&#39; F&#39;!它显示了我设置的信息。

我该如何解决?

-I&#39; m使用mysql workbench 6.3。

提前致谢

2 个答案:

答案 0 :(得分:1)

来自 CREATE TABLE

  

所有存储引擎都会解析public static final float CAMERA_SIZE_RATIO_CALIBRATION = 0.1f; private Camera.Size findTheBestPictureSize() { List<Camera.Size> supportedSizes = mCamera.getParams().getSupportedPictureSizes(); ArrayList<Camera.Size> acceptableSizes = new ArrayList<Camera.Size>(); Camera.Size foundSize = null; Camera.Size tmpSize = null; float desiredRatio = (mDisplaySize[0] * 1f / mDisplaySize[1]); float calculatedRatio; float deltaRatio = 0f; //Looking for the most acceptable sizes int itemCount = supportedSizes.size(); for(int i = 0; i < itemCount; i++) { tmpSize = supportedSizes.get(i); calculatedRatio = (shouldSizeDimensionsBeFlipped ? (tmpSize.height * 1f / tmpSize.width) : (tmpSize.width * 1f / tmpSize.height)); deltaRatio = Math.abs(calculatedRatio - desiredRatio); if(deltaRatio <= CAMERA_SIZE_RATIO_CALIBRATION) { acceptableSizes.add(tmpSize); } } //Looking for the greatest acceptable size itemCount = acceptableSizes.size(); for(int i = 0; i < itemCount; i++) { tmpSize = acceptableSizes.get(i); if(foundSize == null) { foundSize = tmpSize; continue; } if(tmpSize.width > foundSize.width && tmpSize.height > foundSize.height) { foundSize = tmpSize; } } return foundSize; } 子句但忽略

第二

CHECK

SqlFiddleDemo

答案 1 :(得分:1)

使用MySQL ENUM类型。 “ENUM是一个字符串对象,其值从允许值列表中选择,这些值在表创建时在列规范中明确枚举。”

sex ENUM ('M', 'F')