DEV Community

Discussion on: Storing booleans in mySql database as integers/boolean/text for true and false?

Collapse
 
marissab profile image
Marissa B • Edited

I think you're able to use the BIT type in MySQL, which also has the boolean type. Didn't take long to find on a Google search. Stack Overflow has several threads on it over the years.

dev.mysql.com/doc/refman/8.0/en/bi...

When reading your value back from the database, cast it into whatever your UI needs. Usually there's a layer of logic between the crunchy data stuff and fancy interactive UI stuff.

In something like Sqlite which has very limited types available, I go with the text option 'true' and 'false'.

Collapse
 
dgloriaweb profile image
dgloriaweb

storing string "true" or "false" takes more time to return and slower running in a large app, I assume?

Collapse
 
marissab profile image
Marissa B

Not really, no.

Thread Thread
 
dgloriaweb profile image
dgloriaweb • Edited

So you've developed a validation on frontend and backend as well that filters out every typo and other words than true/false? Not being mean here, but that's essential if you choose that road.

Thread Thread
 
marissab profile image
Marissa B

There's no need to do that if you're checking on a situation like whether a checkbox is checked.

If your process involves a user manually entering the words true/false/0/1/etc and you're not validating it before using it on the backend.... That's a big problem for introducing weird issues.

Flipping whatever the checkbox state is into the words true/false to save somewhere should be one line in most languages.

Thread Thread
 
dgloriaweb profile image
dgloriaweb

That's perfectly true. Was overthinking it. :)