mysql经典问题(MySQL null的一些易错点)
mysql经典问题
MySQL null的一些易错点依据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 inliidual 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; |
以上就是MySQL null的一些易错点的详细内容,更多关于MySQL null的资料请关注开心学习网其它相关文章!
原文链接:https://www.omoz.cc/2020/12/11/MySQL%20null%E7%9A%84%E5%AD%A6%E9%97%AE/#null
- mysql开启事务没有提交(MySQL 事务autocommit自动提交操作)
- mysql索引建立及应用(MYSQL创建索引,这些知识应该了解)
- mysql重启启动失败(MySQL8.0无法启动3534的解决方法)
- 安装mysql中要注意什么问题(安装Mysql时可能会遇到的一些疑难杂症)
- 查看mysql执行计划关键字(带你学习MySQL执行计划)
- mysql 索引使用总结(MySQL复合索引的深入探究)
- mysql连接查询原理(MySQL连接查询你真的学会了吗?)
- mysql查看慢查询(MySQL 慢查询日志深入理解)
- docker部署mysql如何访问(完美解决docker安装mysql后Navicat连接不上的问题)
- windows下mysql密码重置(Windows10系统下Mysql8.0.13忘记root密码的操作方法)
- mysql 慢查询日志
- mysql5.7.20非安装版教程(MySQL5.5 部署的一个问题)
- mysqldump 命令详解(mysqldump你可能不知道的参数)
- 执行mysqldump命令后数据库无反应(关于xampp启动不了mysql数据库的解决方法)
- mysql中的null是什么(浅谈为什么Mysql数据库尽量避免NULL)
- mysql高可用集群(MySQL之高可用集群部署及故障切换实现)
- 魔兽世界 设计师爆料,原始版本并无PVP,跨阵营属于返璞归真(魔兽世界设计师爆料)
- 吐槽完《弧光大作战》之后,我们和设计师聊了聊魔兽首款手游的立项初衷和未来(吐槽完弧光大作战之后)
- 魔兽争霸3自定义战役少年杰雷 2(魔兽争霸3自定义战役少年杰雷)
- 今日菜价 芥兰涨幅最高 1.33 ,花菜降幅最高 3.10(今日菜价芥兰涨幅最高)
- 今日菜价 椰菜涨幅最高 3.25 ,水空心菜降幅最高 2.58(今日菜价椰菜涨幅最高)
- 今日菜价 红三鱼涨幅最高 4.41 ,黄鳝降幅最高 5.06(红三鱼涨幅最高)
热门推荐
- css3中的过渡属性的几种方法(基于CSS3实现的几个小loading效果)
- 阿里云服务器购买时考虑的因素(购买阿里云服务器后的基本安全配置)
- checkbox和文字对齐
- css3定义自定义字体(CSS3字体效果的设置方法小结)
- java怎么调用支付宝的扫码支付(python实现银联支付和支付宝支付接入)
- laravel关闭错误提示(解决laravel session失效的问题)
- CSS绝对定位与相对定位
- python自己做个定时器(对python周期性定时器的示例详解)
- dedecms文章权重排序(Dedecms实现tags云标签随机颜色与字体大小方法详解)
- 解释php中可变变量并举例说明(PHP Primary script unknown 解决方法总结)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9