set,enum的數(shù)據(jù)類型都是字符串類型的對(duì)象,其中set最多可以包含64個(gè)元素,并且可以任意取到集合中的元素。而enum則是只能取到集合中的木一個(gè)元素,最多包含65536個(gè)元素,也就是說set是多項(xiàng)選擇,enum是單項(xiàng)選擇了。
這里我們來比較下他們之間相同點(diǎn)和不同點(diǎn):
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)
這里我們看到了它們的輸出結(jié)果,我當(dāng)時(shí)也是很不解后來才知道:
set類型中對(duì)于超出它能表示的范圍的,就用二進(jìn)制來加去:
Set元素
十進(jìn)制
二進(jìn)制
‘x'
1
0001
‘y'
2
0010
‘z'
4
0100
enum類型超出自己能表示的范圍,就附空值了:
enum元素
索引
null
null
‘'
0
‘one'
1
‘two'
2
‘three'
3
現(xiàn)在大家明白了吧。