Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 301
Deviprasad Das Avatar asked May 13 '11 07:05

Deviprasad Das


People also ask

What is the datatype for Boolean in MySQL?

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.

What datatype is used to store Boolean values?

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.

How does MySQL store True False?

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.

Is there BOOLEAN data type in SQL?

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.


2 Answers

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?

like image 142
Kristoffer Lindvall Avatar answered Sep 20 '22 14:09

Kristoffer Lindvall


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.

like image 39
verdesmarald Avatar answered Sep 20 '22 14:09

verdesmarald