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
猜你喜欢
- 一、base64模块base64模块提供了在二进制数据和可打印ASCII字符间编解码的功能,包括 RFC3548中定义的Base16, Ba
- array和asarray都可以将结构数据转化为ndarray,但是主要区别就是当数据源是ndarray时,array仍然会copy出一个副
- 为什么很多站长开始做英文网站,我想主要是原因是良好的互联网环境让大家更容易赚到钱,中小站长做英文网站大致为两类,一是电子商务的外贸网站,二是
- 钟馗之眼是一个强大的搜索引擎,不同于百度谷歌,它主要收集网络中的主机,服务等信息,国内互联网安全厂商知道创宇开放了他们的海量数据库,对之前沉
- 本文实例讲述了Python正则表达式实现简易计算器功能。分享给大家供大家参考,具体如下:需求:使用正则表达式完成一个简易计算器。功能:能够计
- 1、爬取网页分析爬取的目标网址为:https://www.gushiwen.cn/在登陆界面需要做的工作有,获取验证码图片,并识别该验证码,
- 在密码学中,ElGamal加密算法是一个基于迪菲-赫尔曼密钥交换的非对称加密算法。它在1985年由塔希尔·盖莫尔提出。GnuPG和PGP等很
- 下面一段代码给大家带来了python实现12306登录功能,具体代码如下所示:#!/usr/bin/env pythonimport req
- 前言sort包中实现了3种基本的排序算法:插入排序.快排和堆排序.和其他语言中一样,这三种方式都是不公开的,他们只在sort包内部使用.所以
- php中-> 、=>、::、$this->四种符号在代码中很常见,使用很广泛。这篇文章主要介绍了php中-> 、=&
- 在你的 Django 应用中,你或许希望根据某字段的值对检索结果排序,比如说,按字母顺序。 那么,使用 order_by() 这个方法就可以
- 函数原型参数介绍mode (torch.nn.Module, torch.jit.ScriptModule or torch.jit.Scr
- 使用了这么就pip命令,但是一直是简单使用,很多命令都是用了查,查了用,今天把常用的命令汇总一下,方便使用。命令:pip由上图可以看到 pi
- 前言在CSDN发的第一篇文章,时隔两年,终于实现了爬微博的自由!本文可以解决微博预登录、识别“展开全文”并爬取完整数据、翻页设置等问题。由于
- numpy包(模块)几乎总是用于Python中的数值计算。这个软件包为Python提供了高性能的向量、矩阵、张量数据类型。它是在C和Fort
- 这个话题是应腾讯ISD同仁之邀在WebReBuild三周年交流会上做的主题分享。由于临场等原因有些问题当时没有讲明白,回来后按原有思路形成了
- 把value插入dataframe的指定位置loc中,若插入的数据value已在DataFrame中,则返回 错误ValueError,如想
- Python最大的优点之一就是语法简洁,好的代码就像伪代码一样,干净、整洁、一目了然。要写出 Pythonic(优雅的、地道的、整洁的)代码
- 代码如下:function FSOlastline(filename) dim fso,f,temparray
- 1 逻辑数据库和表的设计数据库的逻辑设计、包括表与表之间的关系是优化关系型数据库性能的核心。一个好的逻辑数据库设计可以为优化数据库和应用程序