几天群里面说了ENUM这个类型,顺便看看这个类型的数据在数据库里面是不是按照int类型存放的。

相关资料可以看看《mysql技术内幕innodb存储引擎》第一版或者第二版

第二版103页compact记录格式。下面是我的测试。

表结构:

mysql> desc enum_t;

+-------+---------------------------+------+-----+---------+-------+

| Field | Type                      | Null | Key | Default | Extra |

+-------+---------------------------+------+-----+---------+-------+

| a     | enum('A','B','C','D','E') | YES  |     | NULL    |       |

| b     | char(10)                  | YES  |     | NULL    |       |

+-------+---------------------------+------+-----+---------+-------+

2 rows in set (0.11 sec)

数据:

mysql> select * from enum_t;

+------+------+

| a    | b    |

+------+------+

| A    | B    |

| B    | C    |

| C    | D    |

| D    | E    |

| A    | F    |

| B    | G    |

+------+------+

6 rows in set (0.00 sec)

二进制:

0000c070  73 75 70 72 65 6d 75 6d  00 00 00 10 00 24 00 00  |supremum.....$..|

0000c080  00 00 81 14 00 00 00 00  63 be 93 00 00 01 40 01  |........c.....@.|

0000c090  10 01 42 20 20 20 20 20  20 20 20 20 00 00 00 18  |..B         ....|

0000c0a0  00 24 00 00 00 00 81 15  00 00 00 00 63 bf 94 00  |.$..........c...|

0000c0b0  00 01 c9 01 10 02 43 20  20 20 20 20 20 20 20 20  |......C         |

0000c0c0  00 00 00 20 00 24 00 00  00 00 81 16 00 00 00 00  |... .$..........|

0000c0d0  63 cc 9d 00 00 01 80 01  10 03 44 20 20 20 20 20  |c.........D     |

0000c0e0  20 20 20 20 00 00 00 28  00 24 00 00 00 00 81 17  |    ...(.$......|

0000c0f0  00 00 00 00 63 cd 9e 00  00 01 e2 01 10 04 45 20  |....c.........E |

0000c100  20 20 20 20 20 20 20 20  00 00 00 30 00 24 00 00  |        ...0.$..|

0000c110  00 00 81 18 00 00 00 00  63 da a7 00 00 01 cc 01  |........c.......|

0000c120  10 01 46 20 20 20 20 20  20 20 20 20 00 00 00 38  |..F         ...8|

0000c130  ff 3e 00 00 00 00 81 19  00 00 00 00 63 db a8 00  |.>..........c...|

0000c140  00 01 cd 01 10 02 47 20  20 20 20 20 20 20 20 20  |......G         |

0000c150  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|


可见这个真的是存储了1,2,3,4.