在MySQL中存储布尔值的数据类型是什么? [重复]

时间:2022-09-16 13:21:57

Possible Duplicate:
Which MySQL Datatype to use for storing boolean values?

可能重复:用于存储布尔值的MySQL数据类型是什么?

I am a .NET programmer and using MySQL database for the first time in my life.

我是一名.NET程序员,并且是我生命中第一次使用MySQL数据库。

I wanted to store boolean value, MySQL has BIT, but the .NET conversion of this datatype is UINT64.

我想存储布尔值,MySQL有BIT,但此数据类型的.NET转换是UINT64。

There is another datatype TINYINT(1), whose .NET equivalent is System.Boolean which will serve my purpose.

还有另一种数据类型TINYINT(1),其.NET等价物是System.Boolean,它将满足我的目的。

But why will I use TINYINT(1) (which can store value like 123, 22) instead of BIT, and it will take more space than BIT also (I guess)? It may be legal to use it but I dont think it is etical.

但是为什么我会使用TINYINT(1)(它可以存储123,22这样的值)而不是BIT,它也会占用比BIT更多的空间(我猜)?使用它可能是合法的,但我不认为它是etical。

Can someone please help and clarify my doubt?

有人可以帮助并澄清我的疑问吗?

3 个解决方案

#1


6  

MySQL have BOOL and BOOLEAN, but they are synonyms for TINYINT(1). A value of zero is considered false. Nonzero values are considered true. I guess someone at MySQL thought about it and decided TINYINT(1) is the preferred way to go. I've always used that myself.

MySQL有BOOL和BOOLEAN,但它们是TINYINT(1)的同义词。值为零被视为false。非零值被认为是真实的。我猜MySQL的某个人想到了它并决定TINYINT(1)是首选方式。我一直都在使用它。

There's some more info in this similar question: What is the difference between BIT and TINYINT in MySQL?

在这个类似的问题中还有一些信息:MySQL中的BIT和TINYINT有什么区别?

#2


2  

BIT doesn't work the way you think it does, It is generally used for storing bitfields and BIT(M) takes about (M+7)/8 (integer arithmetic) bytes of storage. Using BIT(1) takes a whole byte anyway, the same as TINYINT so don't worry about it.

BIT不像你想象的那样工作,它通常用于存储位域,BIT(M)用于存储大约(M + 7)/ 8(整数运算)字节。使用BIT(1)无论如何都需要整个字节,与TINYINT相同,所以不用担心它。

In case you are interested, the storage requirements for various types are located here.

如果您有兴趣,可以在此处找到各种类型的存储要求。

#3


1  

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)的别名:

Reference: Which MySQL data type to use for storing boolean values

参考:用于存储布尔值的MySQL数据类型

Similar to MS SQL's Bit Datatype:

与MS SQL的位数据类型类似:

0 = False    1 = True

#1


6  

MySQL have BOOL and BOOLEAN, but they are synonyms for TINYINT(1). A value of zero is considered false. Nonzero values are considered true. I guess someone at MySQL thought about it and decided TINYINT(1) is the preferred way to go. I've always used that myself.

MySQL有BOOL和BOOLEAN,但它们是TINYINT(1)的同义词。值为零被视为false。非零值被认为是真实的。我猜MySQL的某个人想到了它并决定TINYINT(1)是首选方式。我一直都在使用它。

There's some more info in this similar question: What is the difference between BIT and TINYINT in MySQL?

在这个类似的问题中还有一些信息:MySQL中的BIT和TINYINT有什么区别?

#2


2  

BIT doesn't work the way you think it does, It is generally used for storing bitfields and BIT(M) takes about (M+7)/8 (integer arithmetic) bytes of storage. Using BIT(1) takes a whole byte anyway, the same as TINYINT so don't worry about it.

BIT不像你想象的那样工作,它通常用于存储位域,BIT(M)用于存储大约(M + 7)/ 8(整数运算)字节。使用BIT(1)无论如何都需要整个字节,与TINYINT相同,所以不用担心它。

In case you are interested, the storage requirements for various types are located here.

如果您有兴趣,可以在此处找到各种类型的存储要求。

#3


1  

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)的别名:

Reference: Which MySQL data type to use for storing boolean values

参考:用于存储布尔值的MySQL数据类型

Similar to MS SQL's Bit Datatype:

与MS SQL的位数据类型类似:

0 = False    1 = True