MySQL中的NULL和空串的区别

36次阅读
没有评论

共计 3179 个字符,预计需要花费 8 分钟才能阅读完成。

本篇内容主要讲解“MySQL 中的 NULL 和空串的区别”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让丸趣 TV 小编来带大家学习“MySQL 中的 NULL 和空串的区别”吧!

今天接到一个 MySQL 工单,是执行几条 SQL 语句。我一看就感觉这语句比较有意思。
语句大体是这样的:
update app_code_value set channel_id=null where task_id=378 and channel_id=
update app_code_value set channel_id=null where task_id=379 and channel_id=
因为对 Oracle 熟悉一些,所以总是喜欢用 Oracle 的思维来看很多问题,大多数的情况下是相通的,但是还是有一些差别之处。这些就需要额外注意了。
如果用 Oracle 的眼光来看上面的 SQL 语句,那基本可以断定,这个语句就不用执行了。因为在 Oracle 里面 null 和空串还是不同的含义,但是使用起来的效果是一样的。
当然了关于 NULL, 在 MySQL,Oracle 中都是 is null, is not null 这样的语法,这个也是基本的规范。如果使用 =null 这样的情况,效果和 oracle 是一致的。
select count(*)from app_code_value where task_id=378 and channel_id=null;
+———-+
| count(*) |
+———-+
|  0 |
+———-+
1 row in set (0.00 sec)
在 MySQL 里面的 null 和空串是什么情况呢,我们来看看。
使用 is null
select count(*)from app_code_value where task_id=378 and channel_id is null;
+———-+
| count(*) |
+———-+
|  90000 |
+———-+
1 row in set (14.46 sec)
使用空串
select count(*)from app_code_value where task_id=378 and channel_id =
+———-+
| count(*) |
+———-+
|  90000 |
+———-+
1 row in set (14.46 sec)
如果看上面的结果,很容易会以为两者的效果是一致的。我也差点被这种情况误导。我们再来看一个。
select count(*)from app_code_value where task_id=378  and (channel_id is null  or channel_id =
+———-+
| count(*) |
+———-+
|  180000 |
+———-+
1 row in set (5.41 sec)
而直接忽略这个字段是否为空,查看所有匹配的数据,可以看出,也就这些数据了。
select count(*)from app_code_value where task_id=378 ;
+———-+
| count(*) |
+———-+
|  180000 |
+———-+
1 row in set (5.41 sec)
从上面的测试可以看出,null 和空串还是存在一定的差别。如果要形象一点来区分,我看到一个例子很不错,是拿真空和空气的关系来类比空串和 null。

null 和 timestamp

(root:localhost:Wed Jul  6 22:46:46 2016)[test] create table test_null (id int,date timestamp);
insert into test_null values(1,nQuery OK, 0 rows affected (0.16 sec)

(root:localhost:Wed Jul  6 22:46:51 2016)[test] insert into test_null values(1,null);
Query OK, 1 row affected (0.00 sec)

(root:localhost:Wed Jul  6 22:46:51 2016)[test] select *from test_null;
+——+——+
| id  | date |
+——+——+
|  1 | NULL |
+——+——+
1 row in set (0.00 sec)
而要更具体一些来区分,可以使用 length。当然我们可以顺带做一些测试。
create table test_null(id int,name varchar(30));
我们来看看数字类型的表现。
insert into test_null(id) values(null);
insert into test_null(id) values(
select *from test_null;
+——+——+
| id  | name |
+——+——+
| NULL | NULL |
|  0 | NULL |
+——+——+
2 rows in set (0.00 sec)
可以看到数字类型 int 的处理,空串会处理成 0
我们来清空数据,看看字符型的表现。
truncate table test_null;
字符类型插入 null 和空串
insert into test_null(name) values(null);
insert into test_null(name) values(
查看结果如下:
select *from test_null;
+——+——+
| id  | name |
+——+——+
| NULL | NULL |
| NULL |  |
+——+——+
2 rows in set (0.00 sec)
空串的处理还是特别的。空串就是空串。
我们来看看使用 length 来比较这两个字段的结果。
select length(id),id,length(name),name from test_null;
+————+——+————–+——+
| length(id) | id  | length(name) | name |
+————+——+————–+——+
|  NULL | NULL |  NULL | NULL |
|  NULL | NULL |  0 |  |
+————+——+————–+——+
2 rows in set (0.01 sec)
空串的长度是 0,而 null 的长度还是 null,这个和 Oracle 的差别就很明显了。
在 Oracle 中的测试如下:
create table test_null (id number,name varchar2(30));
SQL insert into test_null values(1,null);
1 row created.
SQL insert into test_null values(2,
1 row created.
SQL select *from test_null;
  ID NAME
———- ——————————
  1
  2
SQL select length(id),id,length(name),name from test_null;
LENGTH(ID)  ID LENGTH(NAME) NAME
———- ———- ———— ——————————
  1  1
  1  2

到此,相信大家对“MySQL 中的 NULL 和空串的区别”有了更深的了解,不妨来实际操作一番吧!这里是丸趣 TV 网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-08-01发表,共计3179字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)