You are viewing...

Best Way to Represent Boolean in MySQL

Updated on December 11, 2020 at the 19th hour
Posted under:

DISCLAIMER: Expressed views on this blog are my own.

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.)

I can't say it is good for ORMs though.

You just read "Best Way to Represent Boolean in MySQL". Please share if you liked it!
You can read more recent posts here.