The best data type to store 0, 1, null values in MySQL?


You need to use tinyint(1) unsigned NULL to store the value 0,1 and null values. The syntax is as follows −

yourColumnName TINYINT(1) UNSIGNED NULL;

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

mysql> create table StoreValue0and1orNULLDemo
   -> (
   -> isDigit TINYINT(1) UNSIGNED NULL
   -> );
Query OK, 0 rows affected (0.63 sec)

Now you can insert records 0,1, and NULL in the table using insert command. The query is as follows −

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

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

mysql> select *from StoreValue0and1orNULLDemo;

The following is the output −

+---------+
| isDigit |
+---------+
|       0 |
|       1 |
|    NULL |
+---------+
3 rows in set (0.00 sec)

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

485 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements