开发者

Should I use BIT(1) or BOOL?

开发者 https://www.devze.com 2022-12-23 08:03 出处:网络
Mysql has two types that can hold boolean data, bit and bool. Bit(1) seems more logical, because that has to be 1 or 0, bool is, according to the spe开发者_如何学Pythoncs, the same as saying tinyint(1

Mysql has two types that can hold boolean data, bit and bool. Bit(1) seems more logical, because that has to be 1 or 0, bool is, according to the spe开发者_如何学Pythoncs, the same as saying tinyint(1)


For keeping things semi-universal / portable across other database vendors, use BIT. MySQL's a step ahead of most servers by even allowing the BOOLEAN keyword.

See: Comparison of different SQL implementations


Au contraire, bool seems much more logical, especially if you want to record truth and falsity.


I think it depends on your application and client library. Some database abstraction layers might expect that a boolean is a type of integer. (I know, technically a bit is an integer)


It's also possible to use the CHAR(0) type. A column that is defined as CHAR(0) NULL occupies only one bit and can take only the values NULL and '' (the empty string).

This is documented here: http://dev.mysql.com/doc/refman/5.1/en/string-type-overview.html


bool is, according to the specs, the same as saying tinyint(1)

Does that mean that bool can have more than two possible states (setting aside nullability)? If so, then bit(1) is definitely better, as there's no possibility for ambiguity. What would your app do when it got a value of "3" for a true/false field?

0

精彩评论

暂无评论...
验证码 换一张
取 消