More interviews difference in MySQL NULL and empty values?

Be an active person

Coding, change bug, upgrade their

I have a park-oriented programming, spring!

Snipaste_2019-06-20_13-48-10.png

01 wooden Story

Developed as a background, in their daily work if you want to contact with Mysql database, you will inevitably encounter NULL and empty values ​​in Mysql. Do you know they have any difference?

Learn not move, they do not want to know any difference. Big Brother, ah, to the interview!

A few days ago my good friend to candidates wooden work, he came back and I finished the interview chat with a recollection of his interview questions.


Interviewer: Do you have used MySQL do?

Ogi: There!

Interviewer: Can you say something about the difference in Mysql NULL and null values ​​do?

Wooden :( thinking ...) NULL and empty values ​​are used, you want me to say what's the difference between two, which I really did not think about it, anyway, the actual development will use!

After listening to this wooden answered.

I say: Are you sure this answer is wrong, the question is will you hang up.

Ogi said: NULL translation is not empty it? I really did not carefully thought about, this was quite confusing people.


For other partners do not like my friends here, like wooden fall in the face of this problem, miss favorite company, the following are some differences and talk about a simple order to use both.

02 NULL values ​​and empty

NULL is stored in the field NULL value, a null value is stored in the field null character ( '').

1, space difference

mysql>  select length(NULL), length(''), length('1'); +--------------+------------+-------------+ | length(NULL) | length('') | length('1') | +--------------+------------+-------------+ | NULL | 0 | 1 | +--------------+------------+-------------+ 1 row in set 

Small summary : null seen from above ( '') is a length of 0, space is not; the length of NULL NULL, in fact, it is the space, see the following description.

NULL columns require additional space in the row to record whether their values are NULL.

NULL columns require additional space in the row to record whether their value is NULL.

Popular talk: null state is like a vacuum transfer cup, nothing, but a NULL value is a cup filled with air, although the look is the same, but essentially different.

2, insert / query difference

Create a table,tb_test

CREATE TABLE `tb_test` (
  `one` varchar(10) NOT NULL, `two` varchar(255) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

Insert to verify:

-- 全部插入 NULL,失败
mysql> INSERT tb_test VALUES (NULL,NULL); 1048 - Column 'one' cannot be null 
-- 全部插入 空值,成功
mysql> INSERT tb_test VALUES ('',''); Query OK, 1 row affected 

Simulation data:

INSERT tb_test VALUES (1,NULL);
INSERT tb_test VALUES ('',2); INSERT tb_test VALUES (3,3); 

Blank fields:

-- 使用 is null/is not null
mysql> SELECT * FROM tb_test where one is NULL; Empty set mysql> SELECT * FROM tb_test where one is not NULL; +-----+------+ | one | two | +-----+------+ | 1 | NULL | | | 2 | | 3 | 3 | +-----+------+ 3 rows in set -- 使用 = 、!= mysql> SELECT * FROM tb_test where one = ''; +-----+-----+ | one | two | +-----+-----+ | | 2 | +-----+-----+ 1 row in set mysql> SELECT * FROM tb_test where one != ''; +-----+------+ | one | two | +-----+------+ | 1 | NULL | | 3 | 3 | +-----+------+ 2 rows in set 

NULL value field:

-- 使用 is null/is not null
mysql> SELECT * FROM tb_test where two is not NULL; +-----+-----+ | one | two | +-----+-----+ | | 2 | | 3 | 3 | +-----+-----+ 2 rows in set mysql> SELECT * FROM tb_test where two is NULL; +-----+------+ | one | two | +-----+------+ | 1 | NULL | +-----+------+ 1 row in set -- 使用 = 、!= mysql> SELECT * FROM tb_test where two = ''; Empty set mysql> SELECT * FROM tb_test where two != ''; +-----+-----+ | one | two | +-----+-----+ | | 2 | | 3 | 3 | +-----+-----+ 2 rows in set 

小总结:如果要单纯查NULL值列,则使用 is NULL去查,单纯去查空值('')列,则使用 =''

建议查询方式:NULL值查询使用is null/is not null查询,而空值('')可以使用=或者!=、<、>等算术运算符。

3、COUNT 和 IFNULL函数

使用COUNT函数:

mysql> SELECT count(one) FROM tb_test;
+------------+
| count(one) |
+------------+ | 3 | +------------+ 1 row in set mysql> SELECT count(two) FROM tb_test; +------------+ | count(two) | +------------+ | 2 | +------------+ 1 row in set mysql> SELECT count(*) FROM tb_test; +----------+ | count(*) | +----------+ | 3 | +----------+ 1 row in set 

使用IFNULL函数:

mysql> SELECT IFNULL(one,111111111) from tb_test WHERE one = ''; +-----------------------+ | IFNULL(one,111111111) | +-----------------------+ | | +-----------------------+ 1 row in set mysql> SELECT IFNULL(two,11111111) from tb_test where two is NULL; +----------------------+ | IFNULL(two,11111111) | +----------------------+ | 11111111 | +----------------------+ 1 row in set 

小总结:使用 COUNT(字段) 统计会过滤掉 NULL 值,但是不会过滤掉空值。

说明:IFNULL有两个参数。 如果第一个参数字段不是NULL,则返回第一个字段的值。 否则,IFNULL函数返回第二个参数的值(默认值)。

4、索引字段说明

看到网上有一些人说: MySql中如果某一列中含有NULL,那么包含该列的索引就无效了。

one 和two 字段分别加上普通索引。之前有写过,在复习添加索引:Mysql索引整理总结

-- ALTER TABLE table_name ADD INDEX index_name(col_name);
ALTER TABLE tb_test ADD INDEX index_oat (one, two); ALTER TABLE tb_test add INDEX index_two(two); 

使用 show keys from 表名;show indexes from 表名; ,查看这个表的所有索引信息。

一个普通索引,一个复合索引。

复合索引遵守“最左前缀”原则即在查询条件中使用了复合索引的第一个字段,索引才会被使用。因此,在复合索引中索引列的顺序至关重要。

可以看到,创建了两个索引,并且index_tow NULL 那一列是 YES。

使用EXPLAIN 来进行演示说明,EXPLAIN 的使用说明:Mysql中explain用法和结果字段的含义介绍

复合索引

普通索引

发现查询two字段 是可以正常使用索引的。我使用的MYSQL 5.7 ,InnoDB 引擎。也看了一些网上的资料,MySQL中NULL对索引的影响 这个文章中用例子验证,MySQL可以在含有null的列上使用索引

备注:可能是其他条件下不行,看网上资料说使用空间索引会失效,具体我没有去验证,空间索引没有用到过。查询官网create-index-spatial,感兴趣的伙伴可以自行验证。

这里我想到一点,很多问题的答案都是在指定的条件和环境下才成立,多质疑,多验证。

小总结 :在有NULL值得字段上使用常用的索引,如普通索引、复合索引、全文索引等不会使索引失效。在官网查看在空间索引的情况下,说明了 索引列必须为NOT NULL。

03 总结提升

如果你可以从上面的几个方面和面试官进行一个沟通,即使回答的不是那么的完美,但总比 “这两个都用过,具体有啥区别就不知道了” 这样的回答能好那么一点点。

1、空值不占空间,NULL值占空间。当字段不为NULL时,也可以插入空值。

2、当使用 IS NOT NULL 或者 IS NULL 时,只能查出字段中没有不为NULL的或者为 NULL 的,不能查出空值。

3、判断NULL 用IS NULL 或者 is not null,SQL 语句函数中可以使用IFNULL()函数来进行处理,判断空字符用 =''或者<>''来进行处理。

4、在进行count()统计某列的记录数的时候,如果采用的NULL值,会别系统自动忽略掉,但是空值是会进行统计到其中的。

5, MySql, if a column contains a NULL, then this column contains the index is invalid. This one is not very accurate.

6: actually in the end is a NULL value or a null value ( ''), are distinguished according to the actual traffic. Personal recommendations if there is no specific business scenarios, you can use a null value directly in the actual development.

These are the sort of and I think on this issue, I hope to help to you in the interview. If you have your own thoughts on this topic and understanding, but also welcome message to explore!

04 References

https://www.cnblogs.com/wzmenjoy/p/4244590.html

https://dev.mysql.com/doc/refman/5.7/en/working-with-null.html


Thank you for reading, if you feel this post helpful to you, please praise or love, so that more people see! I wish you a happy happy every day!


 

Whatever you do, stick to it as long as you will see are not the same! On the way, reasonable manner!

 

I wish you all the way life can become the best of themselves, to become a person work independently

© A Feiyun are becoming better every day

Guess you like

Origin www.cnblogs.com/aflyun/p/11125756.html