开发者

what is difference between NULL and Empty in mysql

开发者 https://www.devze.com 2022-12-13 00:01 出处:网络
What is difference betweenNULL and Empty Stringin Mysql ? How much storage space it will take ? For Example .

What is difference between NULL and Empty String in Mysql ?

How much storage space it will take ?

For Example .

In the user table

name : N开发者_开发技巧ULL - How much space its take

phone : -How much space its take


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.


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".


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消