Jump to content

Archived

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

Crashthatch

Storing a boolean

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?

Share this post


Link to post
Share on other sites
Well, there's a BIT field, too -- the question is whether or not this flag is really necessary when a ENUM might be better; you'll need to explain what it's used for.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
I see what you mean. In this case it is just 2 values, but I'll bear it in mind for the future. Thanks for the help.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.