Which MySQL data type to use for storing boolean values

前端 未结 13 1825
感情败类
感情败类 2020-11-22 04:45

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

13条回答
  •  猫巷女王i
    2020-11-22 05:22

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

提交回复
热议问题