免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1090 | 回复: 0
打印 上一主题 下一主题

mysql没有必要枚举整型 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-12-21 10:29 |只看该作者 |倒序浏览

                                               
mysql没有必要枚举整型
有些人习惯地把一些状态表示为整型,在mysql存储这些整型时,却转化成字符串,存在了枚举型的列中。这是不必要的。因为在mysql存储这
些枚举字符串时是把这样字符串做一个序列(index),然后存储相应的index值,比如
Enum_value     index
NULL           NULL    如果NULL是被允许的话
''             0    一切在在插入时,被视为非法值的字符串,插入的都为空
'a'            1
'b'            2
...            ...
也就是说,我们在插入值时,是插入的'a','b'等等字符串,但Mysql真正存储是则是右边的index.如果真得左边的ENUM也为整型值,则是不可取的。因为:
If you store a number into an ENUM column, the number is treated as an index, and the value stored is the enumeration member
with that index. (However, this does not work with LOAD DATA, which treats all input as strings.) It's not advisable to
define an ENUM column with enumeration values that look like numbers, because this can easily become confusing. For example,
the following column has enumeration members with string values of '0', '1', and '2', but numeric index values of
1, 2, and 3.
ENUM的最多的个数也证明了这一点。
An enumeration can have a maximum of 65,535 elements.
同时,都知道程序的需求变化很快,尤其是WEB类的脚本程序。ENUM在表示状态时,也会面临着扩展状态所带来困扰。你必须改变表结构新的值被允许存入。
建议:表示状态类的最好用TINYINT。枚举不是不行,但至少它不是最好。
               
               
               
               
               
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/14250/showart_218992.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP