网站质量,自建虚拟主机网站源码,网站建设公司的市场定位,网站建设套餐有哪些前言#xff1a;在网上看了很多文章#xff0c;大家都是推荐 MySQL 要慎用 ENUM 字段#xff0c;但是原理感觉还是有点模糊。今天我们就从官网来818这些东西1、关于 ENUM 迁移的问题#xff1f;这里引用别人的一段话#xff1a;但ENUM带来的问题也不少#xff0c;比如数据…前言在网上看了很多文章大家都是推荐 MySQL 要慎用 ENUM 字段但是原理感觉还是有点模糊。今天我们就从官网来818这些东西1、关于 ENUM 迁移的问题这里引用别人的一段话但ENUM带来的问题也不少比如数据迁移的时候他几乎不可能被其他数据库所支持如果 ENUM 里面是字符串对于其他数据库来说就更郁闷了还不能设为tinyint等类型的字段2、关于 ENUM 索引的问题纯数字类型的不建议用枚举类型这是因为在 ENUM 内部维护有一个隐形的索引也是按数字排列的容易混淆添加枚举值也是一个问题如果添加在最后还好如果添加在中间什么位置的话原来的隐藏索引将不再起作用Index Values for Enumeration LiteralsEach enumeration value has an index:The elements listed in the column specification are assigned index numbers, beginning with 1.The index value of the empty string error value is 0. This means that you can use the following SELECT statement to find rows into which invalid ENUM values were assigned:mysql SELECT * FROM tbl_name WHERE enum_col0;The index of the NULL value is NULL.The term “index” here refers to a position within the list of enumeration values. It has nothing to do with table indexes.For example, a column specified as ENUM(Mercury, Venus, Earth) can have any of the values shown here. The index of each value is also shown.ValueIndexNULLNULL0Mercury1Venus2Earth3An ENUM column can have a maximum of 65,535 distinct elements. (The practical limit is less than 3000.) A table can have no more than 255 unique element list definitions among its ENUM and SET columns considered as a group. For more information on these limits, see Section C.10.5, “Limits Imposed by .frm File Structure”.If you retrieve an ENUM value in a numeric context, the column values index is returned. For example, you can retrieve numeric values from an ENUM column like this:mysql SELECT enum_col0 FROM tbl_name;Functions such as SUM() or AVG() that expect a numeric argument cast the argument to a number if necessary. For ENUM values, the index number is used in the calculation.3、ENUM 字段 NULL 值问题ENUM 字段默认是可以插入 NULL 值的这个就比较尴尬了而且没有办法优化4、插入的值问题如果插入的值比ENUM设定的值大会默认保存成接近的那个值插入的值不能包含函数不能传递参数numbers ENUM(0,1,2)mysql INSERT INTO t (numbers) VALUES(2),(2),(3);mysql SELECT * FROM t;---------| numbers |---------| 1 || 2 || 2 |---------所以如果插入的值是数字型的建议用tinyint如果插入的值是字符型的建议用char。如果真想用 ENUM 也是可以得前提是要了解到 ENUM 的弊端就可以有效规避这些问题参考文档:https://dev.mysql.com/doc/refman/5.7/en/enum.html#enum-limits为了方便大家交流本人开通了微信公众号和QQ群291519319。喜欢技术的一起来交流吧