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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.