Reputation: 129
What is difference between NULL and Empty String in Mysql ?
How much storage space it will take ?
For Example .
In the user table
name : NULL - How much space its take
phone : -How much space its take
Upvotes: 9
Views: 6825
Reputation: 31225
The best way to think about NULL is that it is poisonous.
Any operation done with NULL will produce NULL.
NULL + any number type is null. NULL concat any string is null.
An empty string is a string with length of 0.
Example:
mysql> select concat('hello world', null);
+-----------------------------+
| concat('hello world', null) |
+-----------------------------+
| NULL |
+-----------------------------+
mysql> select concat('hello world', '');
+---------------------------+
| concat('hello world', '') |
+---------------------------+
| hello world |
+---------------------------+
As for space saving it depends on the datatype the column is defined.
Upvotes: 20
Reputation: 271
Storage space shouldn't be your concern with this. Instead you should be concerned with the semantics of NULL. Let's say I had a glass at my house that you have never seen before in your life. If I ask you how much water is in that glass, you cannot give a valid answer. The truth is that you don't know. Now if I got the glass and showed you there was no water in it, the answer you would give me is "none". NULL is "I don't know the answer" and the empty string is "I do know and the answer is the empty string".
Upvotes: 5
Reputation: 536389
There are valid reasons to use NULL, but saving space in row storage isn't one. If you want a ‘don't know’ placeholder use a NULL; if you simply want to store an empty string that behaves like an empty string, go ahead and store an empty string. If it's a VARCHAR column (which it probably will be) then it's hardly taking much space anyway.
Upvotes: 4