Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BOOLEAN or TINYINT confusion

I was designing a database for a site where I need to use a boolean datetype to store only 2 states, true or false. I am using MySQL.
While designing the database using phpMyAdmin, I found that I have both the BOOLEAN datatype and the TINYINT datatype.
I went through different articles, some said TINYINT is the same as BOOLEAN, no difference. Some say BOOLEAN is converted into TINYINT in MySQL.

MY question is, if they both are same why do there exist two? There should be only one of them.

Here is the reference to the articles I read:
http://www.careerride.com/MySQL-BOOL-TINYINT-BIT.aspx
http://dev.mysql.com/doc/refman/5.5/en/numeric-type-overview.html

like image 649
Bipin Chandra Tripathi Avatar asked Jun 23 '12 07:06

Bipin Chandra Tripathi


People also ask

Why did Boolean change to Tinyint?

The basic difference between Boolean and tinyint(1) is only in the naming convention. If we say that we need true or false values then Boolean comes to our mind, instead of tinyint(1). These data types are synonyms. It is up to us which data type we want to use- values can be 1 and 0 or true and false.

Is Tinyint the same as Boolean?

There is no difference between TINYINT(1) and Boolean. The keyword Bool or Boolean internally converts into TINYINT(1) or we can say Bool or Boolean are synonymous with TINYINT(1).

When should I use Tinyint in MySQL?

TINYINT is a very small integer. The minimum and maximum SIGNED values are -128 and 127 respectively, while for UNSIGNED values TINYINT range is from 0 to 255. TINYINT uses 1 byte per row. It is the best option when you want to save space on your disk and enhance performance.

How do you convert Tinyint to Boolean?

SELECT data FROM table WHERE active will (not should) work as (as the manual says) any non-zero value is considered true. You might also want to consider adding SELECT IF(2, 'true', 'false'); and SELECT IF(0, 'true', 'false'); to your examples.


2 Answers

MySQL does not have internal boolean data type. It uses the smallest integer data type - TINYINT.

The BOOLEAN and BOOL are equivalents of TINYINT(1), because they are synonyms.

Try to create this table -

CREATE TABLE table1 (   column1 BOOLEAN DEFAULT NULL ); 

Then run SHOW CREATE TABLE, you will get this output -

CREATE TABLE `table1` (   `column1` tinyint(1) DEFAULT NULL ) 
like image 159
Devart Avatar answered Oct 15 '22 23:10

Devart


Just a note for php developers (I lack the necessary stackoverflow points to post this as a comment) ... the automagic (and silent) conversion to TINYINT means that php retrieves a value from a "BOOLEAN" column as a "0" or "1", not the expected (by me) true/false.

A developer who is looking at the SQL used to create a table and sees something like: "some_boolean BOOLEAN NOT NULL DEFAULT FALSE," might reasonably expect to see true/false results when a row containing that column is retrieved. Instead (at least in my version of PHP), the result will be "0" or "1" (yes, a string "0" or string "1", not an int 0/1, thank you php).

It's a nit, but enough to cause unit tests to fail.

like image 29
Tom Stambaugh Avatar answered Oct 15 '22 21:10

Tom Stambaugh