开发者

What is the datatype to store boolean value in MySQL? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Which MySQL Datatype to use for storing boolean values?

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

I 开发者_开发百科wanted to store boolean value, MySQL has BIT, but the .NET conversion of this datatype is UINT64.

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

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.

Can someone please help and clarify my doubt?


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.

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


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.

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


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

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

Similar to MS SQL's Bit Datatype:

0 = False    1 = True
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜