Jump to content

Storing a boolean


Crashthatch

Recommended Posts

I need to store a true/false 1/0 value in a mysql database.
Originally, I always used a tinyint field type (1 or 0).
Next I discovered Enum and, thinking it would only take 1 bit rather than 8, switched to using that. (smaller files, faster search time).
I recently read that Enum takes a minimum of 1 byte per record, so there's basically no advantage to either method.

Is there a preferred way of storing boolean values in mysql tables? What is it, and why is that method better?
Link to comment
https://forums.phpfreaks.com/topic/11095-storing-a-boolean/
Share on other sites

One example is in a game I'm writing. Vehicles can either be in the air (eg. a plane) or on the ground (eg. a tank). I use a field "inair" (1 or 0) to determine whether the vehicle is currently airborne. This is then read any time anyone looks at the map.

There are other places too, (building under construction or fully built? weapon has bombard ability?) but I don't see how any of them are any different.

I'll have a look at the bit field.
Link to comment
https://forums.phpfreaks.com/topic/11095-storing-a-boolean/#findComment-41555
Share on other sites

My point was that storing "inair" true/false means you'll need more fields if you need another piece of information as well (e.g. inair vs onground vs something else). Storing a ENUM of air/ground/etc. would be more useful. Same goes for other fields. In my experience, BIT fields are never as useful as they sound, because they're not storing raw information, but rather a very simplistic semantic representation -- which is what the application layer is for.
Link to comment
https://forums.phpfreaks.com/topic/11095-storing-a-boolean/#findComment-41584
Share on other sites

[!--quoteo(post=379660:date=Jun 3 2006, 01:05 PM:name=Crashthatch)--][div class=\'quotetop\']QUOTE(Crashthatch @ Jun 3 2006, 01:05 PM) [snapback]379660[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I need to store a true/false 1/0 value in a mysql database....
[/quote]

There's also the CHAR(0). It stores either the NULL value or the empty string (''). You can easily check the value in PHP with something like is_string($row[0]) or whatever.
Link to comment
https://forums.phpfreaks.com/topic/11095-storing-a-boolean/#findComment-42929
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.