Sunday, 10 June 2007

MySQL says "boo" to booleans

MySQL is supposed to be "all that". Simpler than SQL yet just as powerful (and hey, it's free, so I ain't complaining here)... however, I was pretty surprised to find that there's no data type called "boolean" in MySQL. If you want to do something as simple as store a 'yes' or 'no', you've got to use the enum data type and then create a string of values that will represent your options, eg.

For yes/no radioboxes on a form, you'll need to use
enum('1', '0')
as your data type, where 1 = 'yes' and 0 = 'no', but you could use whatever labels you wanted, eg. 999 = 'yes' and 666 = 'no'.


Mind you, enum is pretty cool, because unlike the narrowminded boolean, enum will allow you to extend your list of options, eg.

For a form with a drop-down box containing the following options:
- Yes
- No
- Occasionally
- Prefer not to answer

You could use
enum('1', '0', '2', '3')
as your data type.

In this example, 'Yes' = 1, 'No' = 0, 'Occasionally' = 2, and 'Prefer not to answer' = 3.

1 comment:

nilesh747 said...

Yes MySQL is cool!
http://tinyurl.com/2hljh6