How can I add a Boolean field to MySQL?

MySQLMySQLi Database

You can use tinyint(1) or bool or boolean. All are synonym. If you use bool or boolean datatype, then it nternally change into tinyint(1).

In PHP, the value 0 represents false and 1 represents true. Any other number except 0 is also true.

Let us check the internal representation of bool or boolean using a table. The query to create a table is as follows.

mysql> create table AddBoolDemo
-> (
-> isToggle bool
-> );
Query OK, 0 rows affected (1.24 sec)

To check the DDL of the table, the following is the query.

SHOW CREATE TABLE yourTableName;

Let us check the representation of bool which internally converts into tinyint(1). Now check for the table AddBoolDemo.

mysql> show create table AddBoolDemo\G

The following is the output.

*************************** 1. row ***************************
Table: AddBoolDemo
Create Table: CREATE TABLE `addbooldemo` (
`isToggle` tinyint(1) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)

Add true or false or 1 or 0 literals in the table using insert command.

mysql> insert into AddBoolDemo values(true);
Query OK, 1 row affected (0.19 sec)

mysql> insert into AddBoolDemo values(false);
Query OK, 1 row affected (0.19 sec)

mysql> insert into AddBoolDemo values(1);
Query OK, 1 row affected (0.10 sec)

mysql> insert into AddBoolDemo values(0);
Query OK, 1 row affected (0.18 sec)

Display all records from the table using select statement. The query is as follows.

mysql> select *from AddBoolDemo;

The following is the output.

+----------+
| isToggle |
+----------+
| 1        |
| 0        |
| 1        |
| 0        |
+----------+
4 rows in set (0.00 sec)
raja
Published on 11-Jan-2019 11:50:06
Advertisements