views:

18427

answers:

5

Since MySQL doesn't seem to have any 'boolean' datatype, which datatype do you 'abuse' for storing true/false information in MySQL? Especially in the context of writing and reading from/to a PHP-Script.

Over time I have used and seen several approaches:

  • tinyint, varchar fields containing the values 0/1,
  • varchar fields containing the strings '0'/'1' or 'true'/'false'
  • and finally enum Fields containing the two options 'true'/'false'.

None of the above seems optimal, I tend to prefer the tinyint 0/1 variant, since automatic type conversion in PHP gives me boolean values rather simply.

So which datatype do you use, is there a type designed for boolean values which I have overlooked? Do you see any advantages/disadvantages by using one type or another?

+5  A: 

I use TINYINT(1) in order to store boolean values in Mysql.

I don't know if there is any advantage to use this... But if i'm not wrong, mysql can store boolean (BOOL) and it store it as a tinyint(1)

http://dev.mysql.com/doc/refman/5.0/en/other-vendor-data-types.html

Fred
That is right. As we all know that a single bit of digital information can either be true or false. so by all means we can use a tiny int for storing Boolean value. OBVIOUSLY.
Jayapal Chandran
+37  A: 

According to the MySQL manual you can use bool and boolean which are at the moment aliases of tinyint(1):

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

MySQL also states that:

We intend to implement full boolean type handling, in accordance with standard SQL, in a future MySQL release.

BTW: this is just a matter of http://www.google.ch/search?q=mysql+boolean+datatype.

tharkun
Yeah, I'd go for either this or, for a CHAR(1) and store 'Y'/'N' or 'T'/'F' etc. depending upon the context. The advantage of using a small integer type is that you get maximum portability across RDBMS-es
Roland Bouman
+7  A: 

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

Philip Morton
+1  A: 

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.

Ciaran McNulty
Your remark concerning the ENUM is not true: try CAST(yourenumcol AS UNSIGNED) and you'll notice that False will be 1, and True will be 2. Another problem with ENUM is that it is too easy to insert '' (empty string) . I would disrecommend using this.
Roland Bouman
A: 

This question has been answered but I figured I'd throw in my $0.02. I often use a CHAR(0), where '' == true and NULL == false.

Josh
That's a really nasty solution, you shouldn't even consider using that.
GuidoH
mm, this seems like asking for trouble if you as me. I mean, depending on the language it may be too easy to not spot the diff between NULL and '' (for example PHP).
Roland Bouman