 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP 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
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)
Advertisements
 