简体   繁体   English

mysql中NULL和Empty有什么区别

[英]what is difference between NULL and Empty in mysql

What is difference between NULL and Empty String in Mysql ? Mysql中NULL和空字符串有什么区别?

How much storage space it will take ? 它需要多少存储空间?

For Example . 例如 。

In the user table 在用户表中

name : NULL - How much space its take name:NULL - 占用多少空间

phone : -How much space its take 电话: - 占用多少空间

The best way to think about NULL is that it is poisonous. 考虑NULL的最好方法是它有毒。

Any operation done with NULL will produce NULL. 使用NULL完成的任何操作都将产生NULL。

NULL + any number type is null. NULL +任何数字类型为null。 NULL concat any string is null. NULL concat任何字符串都为null。

An empty string is a string with length of 0. 空字符串是长度为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. 相反,你应该关注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". NULL是“我不知道答案”,空字符串是“我知道,答案是空字符串”。

There are valid reasons to use NULL, but saving space in row storage isn't one. 有正当理由使用NULL,但在行存储中节省空间不是一个。 If you want a 'don't know' placeholder use a NULL; 如果你想要'不知道'占位符使用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. 如果它是一个VARCHAR列(它可能会是它),那么它几乎没有占用太多空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM