这里我们来比较下他们之间相同点和不同点:
复制代码 代码如下:
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
现在大家明白了吧。