Crashthatch Posted June 3, 2006 Share Posted June 3, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/11095-storing-a-boolean/ Share on other sites More sharing options...
fenway Posted June 3, 2006 Share Posted June 3, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/11095-storing-a-boolean/#findComment-41508 Share on other sites More sharing options...
Crashthatch Posted June 3, 2006 Author Share Posted June 3, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/11095-storing-a-boolean/#findComment-41555 Share on other sites More sharing options...
fenway Posted June 3, 2006 Share Posted June 3, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/11095-storing-a-boolean/#findComment-41584 Share on other sites More sharing options...
Crashthatch Posted June 5, 2006 Author Share Posted June 5, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/11095-storing-a-boolean/#findComment-41904 Share on other sites More sharing options...
Wildbug Posted June 7, 2006 Share Posted June 7, 2006 [!--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. Quote Link to comment https://forums.phpfreaks.com/topic/11095-storing-a-boolean/#findComment-42929 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.