Which MySQL data type to use for storing boolean values

前端 未结 13 1579
感情败类
感情败类 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条回答
  •  清酒与你
    2020-11-22 05:14

    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.

提交回复
热议问题