Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

mysql - 哪个MySQL数据类型用于存储布尔值(Which MySQL data type to use for storing boolean values)

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

(由于MySQL似乎没有任何“布尔”数据类型,你滥用'哪种数据类型'来存储MySQL中的真/假信息?)

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

(特别是在写入和读取PHP脚本的上下文中。)

Over time I have used and seen several approaches:

(随着时间的推移,我使用并看到了几种方法:)

  • tinyint, varchar fields containing the values 0/1,

    (tinyint,包含值0/1的varchar字段,)

  • varchar fields containing the strings '0'/'1' or 'true'/'false'

    (包含字符串'0'/'1'或'true'/'false'的varchar字段)

  • and finally enum Fields containing the two options 'true'/'false'.

    (最后枚举包含两个选项'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.

(我更倾向于使用tinyint 0/1变体,因为PHP中的自动类型转换非常简单地给出了布尔值。)

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?

(您是否看到使用某种类型的优点/缺点?)

  ask by translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For MySQL 5.0.3 and higher, you can use BIT .

(对于MySQL 5.0.3及更高版本,您可以使用BIT 。)

The manual says:

(手册说:)

As of MySQL 5.0.3, the BIT data type is used to store bit-field values.

(从MySQL 5.0.3开始,BIT数据类型用于存储位字段值。)

A type of BIT(M) enables storage of M-bit values.

(一种BIT(M)允许存储M位值。)

M can range from 1 to 64.

(M的范围为1到64。)

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

(否则,根据MySQL手册,你可以使用bool和boolean,它们是tinyint (1)的别名:)

Bool, Boolean: These types are synonyms for TINYINT (1).

(Bool,Boolean:这些类型是TINYINT (1)的同义词。)

A value of zero is considered false.

(值为零被视为false。)

Non-zero values are considered true.

(非零值被认为是真实的。)

MySQL also states that:

(MySQL还声明:)

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

(我们打算在未来的MySQL版本中按照标准SQL实现完整的布尔类型处理。)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...