mysql之set、enum的认识

来源:互联网 发布:java 异步框架 编辑:程序博客网 时间:2024/06/01 10:35

set,enum的数据类型都是字符串类型的对象,其中set最多可以包含64个元素,并且可以任意取到集合中的元素。而enum则是只能取到集合中的木一个元素,最多包含65536个元素,也就是说set是多项选择,enum是单项选择了。

这里我们来比较下他们之间相同点和不同点:

mysql> create table db_set(    -> set1 set('x','y','z') not null,    -> enum1 enum('one','two','three') not null);Query OK, 0 rows affected (0.06 sec)mysql> desc db_set;+-------+---------------------------+------+-----+---------+-------+| Field | Type                      | Null | Key | Default | Extra |+-------+---------------------------+------+-----+---------+-------+| set1  | set('x','y','z')          | NO   |     | NULL    |       || enum1 | enum('one','two','three') | NO   |     | NULL    |       |+-------+---------------------------+------+-----+---------+-------+mysql> insert into db_set values(1,3),(1,4),(4,1);Query OK, 3 rows affected, 1 warning (0.00 sec)Records: 3  Duplicates: 0  Warnings: 1mysql> select * from db_set ;+------+-------+| set1 | enum1 |+------+-------+| x    | three || x    |       || z    | one   |+------+-------+3 rows in set (0.01 sec) 
这里我们看到了它们的输出结果,我当时也是很不解后来才知道:

set类型中对于超出它能表示的范围的,就用二进制来加去:

Set元素

十进制

二进制

x

1

0001

y

2

0010

z

4

0100

enum类型超出自己能表示的范围,就附空值了:

enum元素

索引

null

null

‘’

0

one

1

two

2

three

3


现在大家明白了吧。

原创粉丝点击