Does MySQL converts bool to tinyint(1) internally?


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

You can also say the bool is synonym for tinyint(1). Let us first create a sample table:

mysql> create table boolToTinyIntDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> Name varchar(20),
   -> isAgeGreaterThan18 bool
   -> );
Query OK, 0 rows affected (1.02 sec)

Let us now check the description of table:

mysql> desc boolToTinyIntDemo;

This will produce the following output

+--------------------+-------------+------+-----+---------+----------------+
| Field              | Type        | Null | Key | Default | Extra          |
+--------------------+-------------+------+-----+---------+----------------+
| Id                 | int(11)     | NO   | PRI | NULL    | auto_increment |
| Name               | varchar(20) | YES  |     | NULL    |                |
| isAgeGreaterThan18 | tinyint(1)  | YES  |     | NULL    |                |
+--------------------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

Look at the above sample output, the column isAgeGreaterThan18 data type is converted from bool to tinyint(1) internally.

Following is the query to insert some records in the table using insert command:

mysql> insert into boolToTinyIntDemo(Name,isAgeGreaterThan18) values('Larry',true);
Query OK, 1 row affected (0.18 sec)

mysql> insert into boolToTinyIntDemo(Name,isAgeGreaterThan18) values('Sam',false);
Query OK, 1 row affected (0.14 sec)

Following is the query to display records from the table using select command:

mysql> select *from boolToTinyIntDemo;

This will produce the following output

+----+-------+--------------------+
| Id | Name  | isAgeGreaterThan18 |
+----+-------+--------------------+
| 1  | Larry |                  1 |
| 2  | Sam   |                  0 |
+----+-------+--------------------+
2 rows in set (0.00 sec)

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements