What column type is best to use in a MySQL database for boolean values? I use boolean
but my colleague uses tinyint(1)
.
什么列类型最好在一个MySQL数据库中用于布尔值?我使用布尔型,但我的同事使用tinyint(1)。
6 个解决方案
#1
101
These data types are synonyms.
这些数据类型是同义词。
#2
64
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.
我将在这里采取不同的方法,并建议您的开发人员理解您的代码和编译器/数据库的代码一样重要。使用布尔可以做和使用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。布尔值总是为真或为假。
#3
28
boolean
isn't a distinct datatype in MySQL; it's just a synonym for tinyint
. See this page in the MySQL manual.
布尔型在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作为首选项,因为布尔不像您认为的那样从名称中执行,因此它会导致潜在的误导代码。但在实际的层面上,这并不重要——他们都做同样的事情,所以你也不会用任何东西来获得或失去任何东西。
#4
6
use enum its the easy and fastest
使用enum它的简单和最快
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),因为位(1)只需要1位来存储布尔值,而tinyint(1)需要8位。
ref
裁判
TINYINT vs ENUM(0, 1) for boolean values in MySQL
用于MySQL中的布尔值的TINYINT vs ENUM(0,1)
#5
2
While it's true that bool
and tinyint(1)
are functionally identical, bool
should be the preferred option because it carries the semantic meaning of what you're trying to do. Also, many ORMs will convert bool
into your programing language's native boolean type.
虽然bool和tinyint(1)在功能上是完全相同的,但bool应该是首选,因为它承载了您正在尝试做的事情的语义。此外,许多orm将把bool转换为编程语言的本机布尔类型。
#6
0
My experience when using Dapper to connect to MySQL is 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开始抛出异常。我试着看剧本前后的区别。并且注意到位(1)已更改为tinyint(1)。
I then ran:
然后我跑:
ALTER TABLE TableName CHANGE COLUMN Setting Setting BIT(1) NULL DEFAULT NULL;
Which solved the problem.
解决了这个问题。
#1
101
These data types are synonyms.
这些数据类型是同义词。
#2
64
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.
我将在这里采取不同的方法,并建议您的开发人员理解您的代码和编译器/数据库的代码一样重要。使用布尔可以做和使用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。布尔值总是为真或为假。
#3
28
boolean
isn't a distinct datatype in MySQL; it's just a synonym for tinyint
. See this page in the MySQL manual.
布尔型在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作为首选项,因为布尔不像您认为的那样从名称中执行,因此它会导致潜在的误导代码。但在实际的层面上,这并不重要——他们都做同样的事情,所以你也不会用任何东西来获得或失去任何东西。
#4
6
use enum its the easy and fastest
使用enum它的简单和最快
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),因为位(1)只需要1位来存储布尔值,而tinyint(1)需要8位。
ref
裁判
TINYINT vs ENUM(0, 1) for boolean values in MySQL
用于MySQL中的布尔值的TINYINT vs ENUM(0,1)
#5
2
While it's true that bool
and tinyint(1)
are functionally identical, bool
should be the preferred option because it carries the semantic meaning of what you're trying to do. Also, many ORMs will convert bool
into your programing language's native boolean type.
虽然bool和tinyint(1)在功能上是完全相同的,但bool应该是首选,因为它承载了您正在尝试做的事情的语义。此外,许多orm将把bool转换为编程语言的本机布尔类型。
#6
0
My experience when using Dapper to connect to MySQL is 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开始抛出异常。我试着看剧本前后的区别。并且注意到位(1)已更改为tinyint(1)。
I then ran:
然后我跑:
ALTER TABLE TableName CHANGE COLUMN Setting Setting BIT(1) NULL DEFAULT NULL;
Which solved the problem.
解决了这个问题。