
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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)
Advertisements