mysql 数据类型 枚举类型与集合类型

 

 

字段的值只能在给定范围中选择,如单选框,多选框

enum 单选 只能在给定的范围内选一个值,如性别 sex 男male/女female

 

set 多选 在给定的范围内可以选择一个或一个以上的值(爱好1,爱好2,爱好3...)

 

sex enum('male','female')

hobby set('run','tennis','read','football')

mysql> create table consumer(
    -> id int,
    -> name varchar(16),
    -> sex enum('male','female'),
    -> level enum('vip1','vip2','vip3'),
    -> hobby set('run','tennis','read','football')
    -> );
Query OK, 0 rows affected (0.01 sec)

 

插入一条记录

mysql> insert into consumer values(1,'mike','male','vip1','run,read');
Query OK, 1 row affected (0.00 sec)

mysql> select * from consumer;
+------+------+------+-------+----------+
| id   | name | sex  | level | hobby    |
+------+------+------+-------+----------+
|    1 | mike | male | vip1  | run,read |
+------+------+------+-------+----------+
1 row in set (0.00 sec)

 

插入一条记录时 在sex字段 插入一个不是 male或者female的字段 没有报错

不在范围之内的 最后查询是空的

mysql> insert into consumer values(2,'mike','ssadasd','vip2','tennis,football');
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> select * from consumer;
+------+------+------+-------+-----------------+
| id   | name | sex  | level | hobby           |
+------+------+------+-------+-----------------+
|    1 | mike | male | vip1  | run,read        |
|    2 | mike |      | vip2  | tennis,football |
+------+------+------+-------+-----------------+
2 rows in set (0.00 sec)

 

上一篇:MySQL之外键


下一篇:python csv单行多行写入