Home > Database > Mysql Tutorial > mysql之set与enum的介绍_MySQL

mysql之set与enum的介绍_MySQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-01 13:24:43
Original
1246 people have browsed it

bitsCN.com 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: 1
mysql> 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

现在大家明白了吧。
bitsCN.com

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template