MySQL为何不建议使用默认值为null列
通常能听到的答案是使用了NULL值的列将会使索引失效,但是如果实际测试过一下,你就知道IS NULL会使用索引.所以上述说法有漏洞.
着急的人拉到最下边看结论
Preface
Null is a special constraint of columns.
The columns in table will be added null constrain if you do not define the column with “not null” key words explicitly
when creating the table.Many programmers like to define columns by default
because of the conveniences(reducing the judgement code of nullibility) what consequently
cause some uncertainty of query and poor performance of database.
NULL值是一种对列的特殊约束,我们创建一个新列时,如果没有明确的使用关键字not null声明该数据列,Mysql会默认的为我们添加上NULL约束.
有些开发人员在创建数据表时,由于懒惰直接使用Mysql的默认推荐设置.(即允许字段使用NULL值).而这一陋习很容易在使用NULL的场景中得出不确定的查询结果以及引起数据库性能的下降.
Introduce
Null is null means it is not anything at all,we cannot think of null is equal to ‘' and they are totally different.
MySQL provides three operators to handle null value:“IS NULL”,“IS NOT NULL”,"<=>" and a function ifnull().
IS NULL: It returns true,if the column value is null.
IS NOT NULL: It returns true,if the columns value is not null.
<=>: It's a compare operator similar with “=” but not the same.It returns true even for the two null values.
(eg. null <=> null is legal)
IFNULL(): Specify two input parameters,if the first is null value then returns the second one.
It's similar with Oracle's NVL() function.
NULL并不意味着什么都没有,我们要注意 NULL 跟 ''(空值)是两个完全不一样的值.MySQL中可以操作NULL值操作符主要有三个.
- IS NULL
- IS NOT NULL
- <=> 太空船操作符,这个操作符很像=,select NULL<=>NULL可以返回true,但是select NULL=NULL返回false.
- IFNULL 一个函数.怎么使用自己查吧…反正我会了
Example
Null never returns true when comparing with any other values except null with “<=>”.
NULL通过任一操作符与其它值比较都会得到NULL,除了<=>.
(root@localhost mysql3306.sock)[zlm]>create table test_null( -> id int not null, -> name varchar(10) -> ); Query OK, 0 rows affected (0.02 sec) (root@localhost mysql3306.sock)[zlm]>insert into test_null values(1,'zlm'); Query OK, 1 row affected (0.00 sec) (root@localhost mysql3306.sock)[zlm]>insert into test_null values(2,null); Query OK, 1 row affected (0.00 sec) (root@localhost mysql3306.sock)[zlm]>select * from test_null; +----+------+ | id | name | +----+------+ | 1 | zlm | | 2 | NULL | +----+------+ 2 rows in set (0.00 sec) (root@localhost mysql3306.sock)[zlm]>select * from test_null where name=null; Empty set (0.00 sec) (root@localhost mysql3306.sock)[zlm]>select * from test_null where name is null; +----+------+ | id | name | +----+------+ | 2 | NULL | +----+------+ 1 row in set (0.00 sec) (root@localhost mysql3306.sock)[zlm]>select * from test_null where name is not null; +----+------+ | id | name | +----+------+ | 1 | zlm | +----+------+ 1 row in set (0.00 sec) (root@localhost mysql3306.sock)[zlm]>select * from test_null where null=null; Empty set (0.00 sec) (root@localhost mysql3306.sock)[zlm]>select * from test_null where null<>null; Empty set (0.00 sec) (root@localhost mysql3306.sock)[zlm]>select * from test_null where null<=>null; +----+------+ | id | name | +----+------+ | 1 | zlm | | 2 | NULL | +----+------+ 2 rows in set (0.00 sec) //null<=>null always return true,it's equal to "where 1=1".
栏 目:MySQL
本文地址:https://idc91.com/shujuku/3442.html
您可能感兴趣的文章
- 05-31MySQL中的 inner join 和 left join的区别解析(小结果集驱动大结果集)
- 05-31MySQL索引失效十种场景与优化方案
- 05-31MYSQL 高级文本查询之regexp_like和REGEXP详解
- 05-31MySQL获取binlog的开始时间和结束时间(最新方法)
- 05-31MySQL索引查询的具体使用
- 05-31基于MySQL和Redis扣减库存的实践
- 05-31关于MySQL的存储过程与存储函数
- 05-31MySQL实战文章(非常全的基础入门类教程)
- 05-31MySQL Flink Watermark实现事件时间处理的关键技术
- 05-31MySQL Flink实时流处理的核心技术之窗口机制


阅读排行
推荐教程
- 05-30Navicat for MySQL 11注册码激活码汇总
- 05-27Mysql误删数据快速恢复
- 05-31VS2022连接数据库MySQL并进行基本的表的操作指南
- 05-30解决seata不能使用mysql8版本的问题方法
- 05-30MYSQL字符集设置的方法详解(终端的字符集)
- 05-30解决MySQL启动报错:ERROR 2003 (HY000): Can't con
- 05-30关于Mysql-connector-java驱动版本问题总结
- 11-22mac下安装mysql忘记密码的修改方法
- 05-30MySQL中的隐藏列的具体查看
- 11-22mysql exists与not exists实例详解