用于存储布尔值的哪种MySQL数据类型 [英] Which MySQL data type to use for storing boolean values

查看:82
本文介绍了用于存储布尔值的哪种MySQL数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于MySQL似乎没有任何布尔"数据类型,您会滥用"哪种数据类型在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?

尤其是在与PHP脚本之间进行读写的情况下.

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

随着时间的流逝,我已经使用并看到了几种方法:

Over time I have used and seen several approaches:

  • tinyint,包含值0/1的varchar字段,
  • 包含字符串'0'/'1'或'true'/'false'的varchar字段
  • 最后是枚举包含两个选项'true'/'false'的字段.

以上都不是最佳选择.我倾向于使用tinyint 0/1变体,因为PHP中的自动类型转换使我可以很简单地获得布尔值.

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?

推荐答案

对于MySQL 5.0.3和更高版本,可以使用BIT.手册说:

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

从MySQL 5.0.3开始,BIT数据类型用于存储位字段 价值观.一种类型的BIT(M)可以存储M位的值. M可以范围 从1到64.

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.

否则,根据MySQL手册,您可以使用bool和boolean,它们是

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

布尔型,布尔型:这些类型是 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还指出:

我们打算实现完整的布尔值 类型处理,符合 标准SQL,在未来的MySQL中 释放.

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

参考文献: http://dev.mysql. com/doc/refman/5.5/en/numeric-type-overview.html

这篇关于用于存储布尔值的哪种MySQL数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆