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 does not have a boolean (or bool) data type. Instead, it converts boolean values into integer data types (TINYINT). When you create a table with a boolean data type, MySQL outputs data as 0, if false, and 1, if true.
A Boolean variable has only two possible values: true or false. It is common to use Booleans with control statements to determine the flow of a program. In this example, when the boolean value "x" is true, vertical black lines are drawn and when the boolean value "x" is false, horizontal gray lines are drawn.
MySQL does not contain built-in Boolean or Bool data type. They provide a TINYINT data type instead of Boolean or Bool data types. MySQL considered value zero as false and non-zero value as true. If you want to use Boolean literals, use true or false that always evaluates to 0 and 1 value.
In SQL Server, a Boolean Datatype can be created by means of keeping BIT datatype. Though it is a numeric datatype, it can accept either 0 or 1 or NULL values only. Hence easily we can assign FALSE values to 0 and TRUE values to 1.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With