MySQL 中布尔值的 Boolean 与 tinyint(1)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3751853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:12:11  来源:igfitidea点击:

Boolean vs tinyint(1) for boolean values in MySQL

sqlmysql

提问by tom

What column type is best to use in a MySQL database for boolean values? I use booleanbut my colleague uses tinyint(1).

哪种列类型最适合在 MySQL 数据库中用于布尔值?我用,boolean但我同事用tinyint(1)

回答by Māris Kise?ovs

These data types are synonyms.

这些数据类型是同义词。

回答by dj_segfault

I am going to take a different approach here and suggest that it is just as important for your fellow developers to understand your code as it is for the compiler/database to. Using boolean may do the same thing as using tinyint, however it has the advantage of semantically conveying what your intention is, and that's worth something.

我将在这里采用不同的方法,并建议您的开发人员同事理解您的代码与编译器/数据库理解您的代码同样重要。使用 boolean 可能与使用 tinyint 做同样的事情,但是它具有语义上传达您的意图的优势,这是值得的。

If you use a tinyint, it's not obvious that the only values you should see are 0 and 1. A boolean is ALWAYS true or false.

如果您使用 tinyint,那么您应该看到的唯一值是 0 和 1 并不明显。布尔值始终为真或假。

回答by Spudley

booleanisn't a distinct datatype in MySQL; it's just a synonym for tinyint. See this page in the MySQL manual.

boolean在 MySQL 中不是一个独特的数据类型;它只是 的同义词tinyint请参阅 MySQL 手册中的此页面

Personally I would suggest use tinyint as a preference, because boolean doesn't do what you think it does from the name, so it makes for potentially misleading code. But at a practical level, it really doesn't matter -- they both do the same thing, so you're not gaining or losing anything by using either.

我个人建议使用 tinyint 作为首选项,因为 boolean 不会按照您认为的名称执行操作,因此可能会产生误导性代码。但在实践层面,这真的无关紧要——它们都做同样的事情,所以你不会因为使用它们而获得或失去任何东西。

回答by Pramendra Gupta

use enumits the easy and fastest

使用枚举是最简单和最快的

i will not recommend enum or tinyint(1) as bit(1) needs only 1 bit for storing boolean value while tinyint(1) needs 8 bits.

我不会推荐 enum 或 tinyint(1),因为 bit(1) 只需要 1 位来存储布尔值,而 tinyint(1) 需要 8 位。

ref

参考

TINYINT vs ENUM(0, 1) for boolean values in MySQL

MySQL 中布尔值的 TINYINT 与 ENUM(0, 1)

回答by Kyle Morgan

While it's true that booland tinyint(1)are functionallyidentical, boolshould be the preferred option because it carries the semantic meaning of what you're trying to do. Also, many ORMs will convert boolinto your programing language's native boolean type.

虽然booltinyint(1)功能上确实相同,但bool应该是首选选项,因为它带有您要尝试执行的操作的语义。此外,许多 ORM 将转换bool为您的编程语言的本机布尔类型。

回答by smerlung

My experience when using Dapperto connect to MySQLis that it does matter. I changed a non nullable bit(1) to a nullable tinyint(1) by using the following script:

我在使用Dapper连接到MySQL时的经验是它确实很重要。我使用以下脚本将不可为空的位 (1) 更改为可空的 tinyint(1):

ALTER TABLE TableName MODIFY Setting BOOLEAN null;

Then Dapper started throwing Exceptions. I tried to look at the difference before and after the script. And noticed the bit(1) had changed to tinyint(1).

然后 Dapper 开始抛出异常。我试图查看脚本之前和之后的差异。并注意到 bit(1) 已更改为 tinyint(1)。

I then ran:

然后我跑了:

ALTER TABLE TableName CHANGE COLUMN Setting Setting BIT(1) NULL DEFAULT NULL;

Which solved the problem.

这解决了这个问题。