Which MySQL data type to use for storing boolean values Which MySQL data type to use for storing boolean values mysql mysql

Which MySQL data type to use for storing boolean values


For MySQL 5.0.3 and higher, you can use BIT. The manual says:

As of MySQL 5.0.3, the BIT data type is used to store bit-fieldvalues. A type of BIT(M) enables storage of M-bit values. M can rangefrom 1 to 64.

Otherwise, according to the MySQL manual you can use BOOL or BOOLEAN, which are at the moment aliases of tinyint(1):

Bool, Boolean: These types are synonyms for TINYINT(1). A value ofzero is considered false. Non-zerovalues are considered true.

MySQL also states that:

We intend to implement full booleantype handling, in accordance withstandard SQL, in a future MySQLrelease.

References: http://dev.mysql.com/doc/refman/5.5/en/numeric-type-overview.html


BOOL and BOOLEAN are synonyms of TINYINT(1). Zero is false, anything else is true. More information here.


This is an elegant solution that I quite appreciate because it uses zero data bytes:

some_flag CHAR(0) DEFAULT NULL

To set it to true, set some_flag = '' and to set it to false, set some_flag = NULL.

Then to test for true, check if some_flag IS NOT NULL, and to test for false, check if some_flag IS NULL.

(This method is described in "High Performance MySQL: Optimization, Backups, Replication, and More" by Jon Warren Lentz, Baron Schwartz and Arjen Lentz.)