Ok… this is kind of a dumb question, but what is the best way to store a “true or false” value with mySQL?
Specifically, I have an “enabled” column that will only be set to “yes” or “no”.
Thanks.
Ok… this is kind of a dumb question, but what is the best way to store a “true or false” value with mySQL?
Specifically, I have an “enabled” column that will only be set to “yes” or “no”.
Thanks.
CREATE TABLE (
flag tinyint(1) default 0
)
flag = 0 (false)
flag = 1 (true)
I prefer ENUM(‘N’,‘Y’) DEFAULT ‘N’, as it’s more readable.
I’d just use a 0 or 1. If you have something like “Yes/No,” or “Y/N,” then you need to look for that specifically…like this:
if ($boolean == "Y") {
// do stuff
}
See, that way, you have to type in “Y” or “Yes” or something else like that: you need to save a tiny bit of space in your head to remember what format you’re using, and you have to match it exactly. I use 0 or 1, so that I can do this:
if ($boolean) // do stuff
I don’t have to remember if I’m using Yes/No, Y/N, or On/Off…if it’s 0/1, it’s a tad easier. Just my opinion. It works just as well if you’re using a field that contains either a 0, or any other value, because the condition will return true so long as the value isn’t 0, basically.
I would definetly go for a set value like 0 or 1 and then check it!
Well, technically, if you are looking for the minimum storage requirements then you would choose a CHAR(1) NOT NULL or a TINYINT NOT NULL which each require just one byte for storage. Otherwise, if you can spare a few bytes then it doesn’t really matter.