Which one is better in MySQL - NULL or empty string?

MySQLDatabaseMySQLi

The choice depends upon the database. In ORACLE database, an empty string is converted to NULL.

In MySQL, the usage of an empty string is better as compared to NULL. It is easy to check for an empty string with some boundary conditions, while this cannot be done with NULL. To find NULL, we need to add an extra condition i.e. ‘IS NULL’

We can check that the length of NULL is 0 while length of empty string is 1.

To check the length of NULL.

mysql>SELECT count(NULL);

The following is the output of the above query.

+-----------------+
| count(NULL)     |
+-----------------+
| 0               |
+-----------------+
1 row in set (0.05 sec)

Therefore, the length of NULL is 0 in MySQL.

To check the length of an empty string.

mysql>SELECT count('');

The following is the output.

+-----------+
| count('') |
+-----------+

| 1         |
+-----------+
1 row in set (0.00 sec)

It shows that the length of an empty string is 1.

raja
Published on 20-Nov-2018 11:03:16
Advertisements