MySQL的哪些数据类型用于存储布尔值?
从MySQL似乎没有任何'布尔'数据类型,数据类型而你'虐待'的MySQL的存储真/假信息?尤其是在写作的背景和阅读从/到一个PHP脚本。
Since MySQL doesn't seem to have any 'boolean' datatype, which datatype 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字段0/1,
- 包含字符串'0'/'1'或'真正的'/'假' VARCHAR字段
- 和含最后枚举字段两个选项真/假。
- 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'.
上面似乎没有最优,我倾向于preFER的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 datatype 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数据类型用于存储位字段
值。 A型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.
另外,根据您可以使用布尔和布尔这是在TINYINT的时刻别名MySQL手册(1):
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.
BTW:这只是一个https://google.com/search?q=mysql+boolean+datatype.
BTW: this is just a matter of https://google.com/search?q=mysql+boolean+datatype.
的搞笑是吧,这个环节,贴在几年前,已成为递归。的