零零乙 2008-11-14 10:36 采纳率: 33.3%
浏览 316
已采纳

用于存储布尔值的 MySQL 数据类型

Since MySQL doesn't seem to have any 'boolean' data type, which data type do you 'abuse' for storing true/false information in MySQL?

Especially in the context of writing and reading from/to a PHP script.

Over time I have used and seen several approaches:

  • tinyint, varchar fields containing the values 0/1,
  • varchar fields containing the strings '0'/'1' or 'true'/'false'
  • and finally enum Fields containing the two options 'true'/'false'.

None of the above seems optimal. I tend to prefer the tinyint 0/1 variant, since automatic type conversion in PHP gives me boolean values rather simply.

So which data type do you use? Is there a type designed for boolean values which I have overlooked? Do you see any advantages/disadvantages by using one type or another?

转载于:https://stackoverflow.com/questions/289727/which-mysql-data-type-to-use-for-storing-boolean-values

  • 写回答

11条回答 默认 最新

  • 零零乙 2008-11-14 10:50
    关注

    For MySQL 5.0.3 and higher, you can use BIT. The manual says:

    As of MySQL 5.0.3, the BIT data type is used to store bit-field values. A type of BIT(M) enables storage of M-bit values. M can range from 1 to 64.

    Otherwise, according to the MySQL manual you can use bool and boolean which are at the moment aliases of tinyint(1):

    Bool, Boolean: These types are synonyms for TINYINT(1). A value of zero is considered false. Non-zero values are considered true.

    MySQL also states that:

    We intend to implement full boolean type handling, in accordance with standard SQL, in a future MySQL release.

    References: http://dev.mysql.com/doc/refman/5.5/en/numeric-type-overview.html

    BTW: this is just a matter of https://google.com/search?q=mysql+boolean+datatype.

    Funny isn't it, this link, posted a few years back, has become recursive.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(10条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?