Since MySQL doesn\'t seem to have any \'boolean\' data type, which data type do you \'abuse\' for storing true/false information in MySQL?
Especially in the context
Until MySQL implements a bit datatype, if your processing is truly pressed for space and/or time, such as with high volume transactions, create a TINYINT field called bit_flags
for all your boolean variables, and mask and shift the boolean bit you desire in your SQL query.
For instance, if your left-most bit represents your bool field, and the 7 rightmost bits represent nothing, then your bit_flags
field will equal 128 (binary 10000000). Mask (hide) the seven rightmost bits (using the bitwise operator &
), and shift the 8th bit seven spaces to the right, ending up with 00000001. Now the entire number (which, in this case, is 1) is your value.
SELECT (t.bit_flags & 128) >> 7 AS myBool FROM myTable t;
if bit_flags = 128 ==> 1 (true)
if bit_flags = 0 ==> 0 (false)
You can run statements like these as you test
SELECT (128 & 128) >> 7;
SELECT (0 & 128) >> 7;
etc.
Since you have 8 bits, you have potentially 8 boolean variables from one byte. Some future programmer will invariably use the next seven bits, so you must mask. Don’t just shift, or you will create hell for yourself and others in the future. Make sure you have MySQL do your masking and shifting — this will be significantly faster than having the web-scripting language (PHP, ASP, etc.) do it. Also, make sure that you place a comment in the MySQL comment field for your bit_flags
field.
You’ll find these sites useful when implementing this method:
Referring to this link Boolean datatype in Mysql, according to the application usage, if one wants only 0 or 1 to be stored, bit(1) is the better choice.
I got fed up with trying to get zeroes, NULLS, and '' accurately round a loop of PHP, MySql and POST values, so I just use 'Yes' and 'No'.
This works flawlessly and needs no special treatment that isn't obvious and easy to do.
BOOL
and BOOLEAN
are synonyms of TINYINT(1)
. Zero is false
, anything else is true
. More information here.
If you use the BOOLEAN type, this is aliased to TINYINT(1). This is best if you want to use standardised SQL and don't mind that the field could contain an out of range value (basically anything that isn't 0 will be 'true').
ENUM('False', 'True') will let you use the strings in your SQL, and MySQL will store the field internally as an integer where 'False'=0 and 'True'=1 based on the order the Enum is specified.
In MySQL 5+ you can use a BIT(1) field to indicate a 1-bit numeric type. I don't believe this actually uses any less space in the storage but again allows you to constrain the possible values to 1 or 0.
All of the above will use approximately the same amount of storage, so it's best to pick the one you find easiest to work with.
After reading the answers here I decided to use bit(1)
and yes, it is somehow better in space/time, BUT after a while I changed my mind and I will never use it again. It complicated my development a lot, when using prepared statements, libraries etc (php).
Since then, I always use tinyint(1)
, seems good enough.