枚举
枚举类型规定了取值的数量和内容,这对于需要限定取值的属性非常有帮助。比如性别有三种,男、女、未知,是否打印有两种,是,否。
package com.gxz.entities;
public enum Color {
RED,
GREEN,
BLUE
}
数据库表
CREATE TABLE MyClothes (
MyClothesId BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
Brand VARCHAR(255) NOT NULL,
Colour ENUM('RED','GREEN','BLUE'),
INDEX MyClothesBrand(Brand)
) ENGINE = InnoDB;
DBMS使用MySQL,字段Colour是枚举类型,只能取值'RED','GREEN','BLUE',若是插入其他值,报出异常。
Operation failed: There was an error while applying the SQL script to the database.
Executing:
INSERT INTO `entitymappings`.`myclothes` (`MyClothesId`, `Brand`, `Colour`) VALUES ('1', '佐丹奴', 'BLANK');
ERROR 1265: 1265: Data truncated for column 'Colour' at row 1
SQL Statement:
INSERT INTO `entitymappings`.`myclothes` (`MyClothesId`, `Brand`, `Colour`) VALUES ('1', '佐丹奴', 'BLANK')
实体类
pack