Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

True/False vs 0/1 in MySQL

Tags:

mysql

boolean

Which is faster in a MySQL database? Booleans, or using zero and one to represent boolean values? My frontend just has a yes/no radio button.

like image 221
SomeKittens Avatar asked Jun 01 '12 14:06

SomeKittens


People also ask

Is 0 true or false in MySQL?

MySQL defines a value of 0 as false and a non-zero value as true. Therefore, to use Boolean literal values, you use the constants TRUE and FALSE that evaluate the value of 0 and 1. From the above, we can deduce that MySQL considers 1 and 0 as True and False, respectively.

Is 1 in MySQL True or false?

MySQL does not have a boolean (or bool) data type. Instead, it converts boolean values into integer data types (TINYINT). When you create a table with a boolean data type, MySQL outputs data as 0, if false, and 1, if true.

Is SQL false 0 or 1?

A Boolean table column will contain either string values of "True" and "False" or the numeric equivalent representation, with 0 being false and 1 being true.

Is true equal to 1 in SQL?

There are no built-in values true and false . One alternative is to use strings 'true' and 'false' , but these are strings just like any other string. Often the bit type is used instead of Boolean as it can only have values 1 and 0 . Typically 1 is used for "true" and 0 for "false".


2 Answers

Some "front ends", with the "Use Booleans" option enabled, will treat all TINYINT(1) columns as Boolean, and vice versa.

This allows you to, in the application, use TRUE and FALSE rather than 1 and 0.

This doesn't affect the database at all, since it's implemented in the application.

There is not really a BOOLEAN type in MySQL. BOOLEAN is just a synonym for TINYINT(1), and TRUE and FALSE are synonyms for 1 and 0.

If the conversion is done in the compiler, there will be no difference in performance in the application. Otherwise, the difference still won't be noticeable.

You should use whichever method allows you to code more efficiently, though not using the feature may reduce dependency on that particular "front end" vendor.

like image 91
Marcus Adams Avatar answered Sep 24 '22 02:09

Marcus Adams


In MySQL TRUE and FALSE are synonyms for TINYINT(1).

So therefore its basically the same thing, but MySQL is converting to 0/1 - so just use a TINYINT if that's easier for you

P.S.
The performance is likely to be so minuscule (if at all), that if you need to ask on StackOverflow, then it won't affect your database :)

like image 20
Laurence Avatar answered Sep 22 '22 02:09

Laurence