MySQL null的一些易错点
作者:OMOZ 发布时间:2024-01-22 13:00:30
依据null-values,MySQL的值为null的意思只是代表没有数据,null值和某种类型的零值是两码事,比如int类型的零值为0,字符串的零值为””,但是它们依然是有数据的,不是null.
我们在保存数据的时候,习惯性的把暂时没有的数据记为null,表示当前我们无法提供有效的信息.
不过使用null但是时候,需要我们注意一些问题.对此MySQL文档说明如下: problems-with-null
使用null的易错点
下面我摘取MySQL官方给出的null的易错点做讲解.
对MySQL不熟悉的人很容易搞混null和零值
The concept of the NULL value is a common source of confusion for newcomers to SQL
比如下面这2句SQL产生的数据是独立的
mysql> INSERT INTO my_table (phone) VALUES (NULL);
mysql> INSERT INTO my_table (phone) VALUES ('');
第一句SQL只是表示暂时不知道电话号码是多少,第二句是电话号码知道并且记录为''
Both statements insert a value into the phone column, but the first inserts a NULL value and the second inserts an empty string. The meaning of the first can be regarded as “phone number is not known” and the meaning of the second can be regarded as “the person is known to have no phone, and thus no phone number.”
对null的逻辑判断要单独处理
对于是否为null的判断必须使用专门的语法IS NULL
,IS NOT NULL
,IFNULL()
.
To help with NULL handling, you can use the IS NULL and IS NOT NULL operators and the IFNULL() function.
如果你使用=判断,那么永远是false
In SQL, the NULL value is never true in comparison to any other value, even NULL
To search for column values that are NULL, you cannot use an expr = NULL test. The following statement returns no rows, because expr = NULL is never true
比如你这样写,where后判断的结果永不会是true:
SELECT * FROM my_table WHERE phone = NULL;
如果你使用null和其他数据做计算,那么结果永远是null,除非MySQL文档对某些操作做了额外的特殊说明
An expression that contains NULL always produces a NULL value unless otherwise indicated in the documentation for the operators and functions involved in the expression
例如:
mysql> SELECT NULL, 1+NULL, CONCAT('Invisible',NULL);
+------+--------+--------------------------+
| NULL | 1+NULL | CONCAT('Invisible',NULL) |
+------+--------+--------------------------+
| NULL | NULL | NULL |
+------+--------+--------------------------+
1 row in set (0.00 sec)
所以你要对null做逻辑判断,还是乖乖的使用IS NULL
To look for NULL values, you must use the IS NULL test
对有null值的列做索引要额外预料到隐藏的细节
只有InnoDB,MyISAM,MEMORY 存储引擎支持给带有null值的列做索引
You can add an index on a column that can have NULL values if you are using the MyISAM, InnoDB, or MEMORY storage engine. Otherwise, you must declare an indexed column NOT NULL, and you cannot insert NULL into the column.
索引的长度会比普通索引大1,也就是略微耗内存点
Due to the key storage format, the key length is one greater for a column that can be NULL than for a NOT NULL column.
对null值做分组,去重,排序会被特殊对待
和上文讲的=null永远是false相反,这时null 被认为是相等的.
When using DISTINCT, GROUP BY, or ORDER BY, all NULL values are regarded as equal.
对null排序会被特殊对待
null值要么被排在最前面,要么最后面
When using ORDER BY, NULL values are presented first, or last if you specify DESC to sort in descending order.
聚合操作时null被忽略
Aggregate (group) functions such as COUNT(), MIN(), and SUM() ignore NULL values
例如count(*)不会统计值为null的数据.
The exception to this is COUNT(*), which counts rows and not individual column values. For example, the following statement produces two counts. The first is a count of the number of rows in the table, and the second is a count of the number of non-NULL values in the age column:
mysql> SELECT COUNT(*), COUNT(age) FROM person;
来源:https://www.omoz.cc/2020/12/11/MySQL%20null%E7%9A%84%E5%AD%A6%E9%97%AE/#null


猜你喜欢
- 看到网上有一篇文章,是介绍如何做网站测试的.从一开始的链接测试,页面内容测试,到浏览器兼容性测试,负载压力测试,一直到最后的安全性测试,甚至
- 版本更新,原来user里的password字段已经变更为authentication_string版本更新 缘故,好多网上的教程都不适用了,
- 运行代码框<SCRIPT>var oPopup = window.createPopup();var popTop=50;fun
- 摘要:Ffrpc可以很方便的构建c++ server, 在网游服务器程序开发中,进程间通讯非常的重要,比如gateserver和gamese
- 1.什么是JDBCJDBC是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口
- 关于这个问题的解答,网络上有成千上万个版本,当然,出现问题的诱因不同,解决的途径也不同,所以我不排除其他解决此类问题的办法,但是这些办法都没
- 什么是Nacos英文全称Dynamic Naming and Configuration Service,Na为naming/nameSer
- 本文代码重点在于演示Python扩展库matplotlib.pyplot中fill_between()函数的用法。import numpy
- 本文实例讲述了Python实现的根据IP地址计算子网掩码位数功能。分享给大家供大家参考,具体如下:#!/usr/bin/env python
- OS ( Operating System 操作系统 ) 操作系统模块;它是属于python的标准库,也就是Python自带的库;它常用于处
- Mysql 查询缓存 查询缓存的作用就是当查询接收到一个和之前同样的查询,服务器将会从查询缓存种检索结果,而不是再次分析和执行上次的查询。这
- 从这篇开始,用一个新的系列文章去介绍和学习MySQL。为什么要学习MySQL,从产品角度来说,大部分软件的数据库服务还是采取关系型数据库,主
- 感谢Mr.Cool 给asp之家投递精彩的文章!站长你好,我经常光临你的站点,因为我也特别喜欢ASP写网页,你的站点上提供的代码比较适用哈,
- # encoding:utf-8import re # 使用正则 匹配想要的数据import requests # 使用requests得到
- 在网络设备管理中,传输配置文件、镜像文件等是经常需要进行的操作。Netmiko是一个Python库,可用于与各种网络设备进行交互,提供了一些
- PDOStatement::fetchObjectPDOStatement::fetchObject — 获取下一行并作为一个对象返回。(P
- 在学习和使用各种数据库的过程中,我们常常会遇到聚族索引、非聚族索引、组合索引的概念,这些索引对我们使用数据库,特别是查询的速度的
- 这些导航菜单来自于Dribbble网站,出自于世界各地的优秀设计师之手,涵盖了各种不同的风格,个个都非常精美。这里我将这些导航菜单展示出来,
- 朋友的网站要计算机票的折扣价格,并且在最后的折扣价格上应对个位进行四舍五入,同时在ASP和Javasc
- 前言:由于做项目需要一个树形选择器,项目用的也是element-ui框架,然而它自带的选择器组件没有树形选项,又不想引入其他的框架组件,于是