
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Which one is better to insert NULL or empty string in MySQL?
In innoDB, NULL occupies less space as compared to empty string. Also, the NULL length is null while length of the empty string is 0.
To understand this, a table is created with the help of create command which is given as follows −
mysql> CREATE table DemoEmptyAndNULL -> ( -> Message varchar(100) -> ); Query OK, 0 rows affected (0.49 sec)
After creating the table successfully, an empty record is inserted into the table with the help of insert command which is as follows −
mysql> INSERT into DemoEmptyAndNULL values(' '); Query OK, 1 row affected (0.17 sec)
After inserting the record, we can display all the records with the help of the select command which is as follows −
mysql> SELECT * from DemoEmptyAndNULL;
After executing the above query, following output is obtained −
+---------+ | Message | +---------+ | | +---------+ 1 row in set (0.00 sec)
The syntax to check the length of an empty string with the help of a built in function count is as follow −
select count(column_name) from yourTableName;
The above syntax is used to get the length of an empty string which is given as follows −
mysql> select count(message) from DemoEmptyAndNULL;
After executing the above query, the following is the output −
+----------------+ | count(message) | +----------------+ | 1 | +----------------+ 1 row in set (0.06 sec)
From the above output it is clear that the length of the empty string is 1.
Now we will check the length of NULL. First, the record that was inserted into the table is deleted using delete command as follows −
mysql> delete from DemoEmptyAndNULL where message=' '; Query OK, 1 row affected (0.19 sec)
The following query is used to check that there is no record in the table right now
mysql> SELECT * from DemoEmptyAndNULL; Empty set (0.00 sec)
Now, the record with null value is inserted into the table as follows −
mysql> INSERT into DemoEmptyAndNULL values(); Query OK, 1 row affected (0.18 sec)
The record is displayed using the select command and the output obtained is as follows −
+---------+ | Message | +---------+ | NULL | +---------+ 1 row in set (0.00 sec)
Now, there is only one record with null value in the table. To find its length following query is used −
mysql> select count(message) from DemoEmptyAndNULL;
The following is the output of the above query −
+----------------+ | count(message) | +----------------+ | 0 | +----------------+ 1 row in set (0.00 sec)
The above output means that count is 0 for null value.
Let us check the MySQL version.
mysql> select version(); +-----------+ | version() | +-----------+ | 8.0.12 | +-----------+ 1 row in set (0.06 sec)
So it is better to use empty string for database as allowing NULL value forces the system to do extra work and does not give the data that you are looking for. However, NULL does not throw any exception when the count() function is executed.
- Related Articles
- Which one is better in MySQL - NULL or empty string?
- How it is possible to insert a zero or an empty string into a MySQL column which is defined as NOT NULL?
- Which one is better POW() or POWER() in MySQL?
- How to test String is null or empty?
- Why it shows 0 instead of empty string whenever I insert an empty string into a MySQL column which is declared as NOT NULL?
- How to update empty string to NULL in MySQL?
- How to check if field is null or empty in MySQL?
- Check if a String is empty ("") or null in Java
- Empty string in not-null column in MySQL?
- What role data type plays when I insert an empty string into a MySQL column which is declared as NOT NULL?
- Check whether a field is empty or null in MySQL?
- Java Program to Check if a String is Empty or Null
- Golang program to check if a string is empty or null
- Check if a String is whitespace, empty ("") or null in Java
- Java program to find whether the given string is empty or null
