mysql数据类型有布尔么,MySQL的哪些数据类型用于存储布尔值?

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 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 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?

解决方案

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.

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值