久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

MySQL中的NULL和空串的區別

150次閱讀
沒有評論

共計 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字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 普定县| 分宜县| 濮阳市| 汶川县| 祁门县| 许昌市| 天气| 灵寿县| 仪征市| 莱芜市| 建德市| 成安县| 葵青区| 潍坊市| 青田县| 科技| 东安县| 页游| 罗源县| 镇远县| 鄯善县| 阳谷县| 阿瓦提县| 五河县| 黑龙江省| 甘孜| 阿荣旗| 肥西县| 济南市| 偃师市| 福泉市| 北海市| 增城市| 晋江市| 三河市| 汉川市| 双鸭山市| 白山市| 凤翔县| 兴化市| 眉山市|