What is the smallest datatype for one bit in MySQL?

MySQLMySQLi Database

The smallest datatype for one bit can be bit(1). The syntax is as follows −

yourColumnName bit(1)

To understand the above syntax, let us create a table. The query to create a table is as follows −

mysql> create table bitDemo
   -> (
   -> isValid bit(1)
   -> );
Query OK, 0 rows affected (0.49 sec)

Now you can check all the details of table with the help of SHOW CREATE command. The query is as follows −

mysql> show create table bitDemo;

Here is the output −

+---------+-----------------------------------------------------------------------------------------------------------------------------+
| Table   | Create Table                                                                                                                |
+---------+-----------------------------------------------------------------------------------------------------------------------------+
| bitDemo | CREATE TABLE `bitdemo` (`isValid` bit(1) DEFAULT NULL) ENGINE =InnoDB DEFAULT CHARSET =utf8mb4 COLLATE =utf8mb4_0900_ai_ci  |
+---------+-----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

Insert some records in the table using insert command. The query is as follows −

mysql> insert into bitDemo values(0);
Query OK, 1 row affected (0.13 sec)
mysql> insert into bitDemo values(1);
Query OK, 1 row affected (0.10 sec)
mysql> insert into bitDemo values(1);
Query OK, 1 row affected (0.07 sec)
mysql> insert into bitDemo values(0);
Query OK, 1 row affected (0.14 sec)

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

mysql> select *from bitDemo;

Here is the output −

+---------+
| isValid |
+---------+
|         |
|         |
|         |
|         |
+---------+
4 rows in set (0.00 sec)

To show the bit value, use the following query −

mysql> select isValid+0 from bitDemo;

The following is the output −

+-----------+
| isValid+0 |
+-----------+
| 0         |
| 1         |
| 1         |
| 0         |
+-----------+
4 rows in set (0.00 sec)
raja
Published on 01-Apr-2019 15:54:58
Advertisements