枚举类型与集合类型
字段的值只能在给定范围中选择,如单选框,多选框
enum 单选 只能在给定的范围内选一个值,如性别 sex 男male/女female
set 多选 在给定的范围内可以选择一个或一个以上的值(爱好1,爱好2,爱好3...)
create table consumer( id int, name char(16), sex enum('male','female'), level enum ('vip1','vip2','vip3'), hobbies set('play','music','run') ); insert into consumer values (1,'cmz','male','vip1','music,run'); # 因为不在选择范围内会报错 insert into consumer values (2,'cmz','xxx','vip1','music,run');
# 操作过程 mysql> create table consumer( -> id int, -> name char(16), -> sex enum('male','female'), -> level enum ('vip1','vip2','vip3'), -> hobbies set('play','music','run') -> ); Query OK, 0 rows affected (0.03 sec) mysql> insert into consumer values -> (1,'cmz','male','vip1','music,run'); Query OK, 1 row affected (0.04 sec) mysql> select * from consumer; +------+------+------+-------+-----------+ | id | name | sex | level | hobbies | +------+------+------+-------+-----------+ | 1 | cmz | male | vip1 | music,run | +------+------+------+-------+-----------+ 1 row in set (0.00 sec) mysql> insert into consumer values -> (1,'cmz','xxx','vip1','music,run'); ERROR 1265 (01000): Data truncated for column 'sex' at row 1