Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why MySQL interprets Boolean as TINYINT(1) instead of BIT(1)?

Tags:

mysql

When considering only two possible values, 0 & 1 or True & False, it is quite obvious that BIT(1) does a better job:

  • BIT(1) forces only 2 possible values : 0 and 1, while TINYINT(1) can accept any integer values less than 10 (0,1,2,3,4,5....) which can be ambiguous.
  • Multiple BIT(1) columns can be combined into bytes so they require less space than multiple TINYINT(1) columns do.

So why MySQL interprets Boolean as TINYINT(1), but not BIT(1)? Is there any advantage of using TINYINT(1) over BIT(1) in handling boolean values?

like image 516
LazNiko Avatar asked Dec 29 '11 17:12

LazNiko


People also ask

Why Boolean in MySQL is 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.

Why does Boolean become Tinyint?

Yes, MySQL internally convert bool to tinyint(1) because tinyint is the smallest integer data type.

Is Tinyint 1 is 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).

What does Tinyint 1 mean?

The number 1 used in parenthesis is only for width display. The INT(1) and TINYINT(1) does not influence the storage. The TINYINT takes 1 byte that means it has range -128 to +127 while int takes 4 bytes; it has range -2147483648 to +2147483647.


1 Answers

It depends on version and database engine and driver

  • BIT is supported properly in 5.05+ with MyISAM and InnoDB
  • Some JDBC drivers need to be told this (eg Kettle's bundled driver)

But BIT is preferable to TINYINT of course.
It's just legacy and inertia that keeps TINYINT...

like image 188
gbn Avatar answered Oct 15 '22 13:10

gbn