溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Oracle中Null與空字符串的區(qū)別是什么

發(fā)布時(shí)間:2021-07-26 16:43:03 來(lái)源:億速云 閱讀:320 作者:Leah 欄目:數(shù)據(jù)庫(kù)

本篇文章為大家展示了Oracle中Null與空字符串的區(qū)別是什么,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

含義解釋?zhuān)?nbsp;
問(wèn):什么是NULL? 
答:在我們不知道具體有什么數(shù)據(jù)的時(shí)候,也即未知,可以用NULL,我們稱(chēng)它為空,ORACLE中,含有空值的表列長(zhǎng)度為零。 
ORACLE允許任何一種數(shù)據(jù)類(lèi)型的字段為空,除了以下兩種情況: 
1、主鍵字段(primary key), 
2、定義時(shí)已經(jīng)加了NOT NULL限制條件的字段 

說(shuō)明: 
1、等價(jià)于沒(méi)有任何值、是未知數(shù)。 
2、NULL與0、空字符串、空格都不同。 
3、對(duì)空值做加、減、乘、除等運(yùn)算操作,結(jié)果仍為空。 
4、NULL的處理使用NVL函數(shù)。 
5、比較時(shí)使用關(guān)鍵字用“is null”和“is not null”。 
6、空值不能被索引,所以查詢(xún)時(shí)有些符合條件的數(shù)據(jù)可能查不出來(lái),count(*)中,用nvl(列名,0)處理后再查。 
7、排序時(shí)比其他數(shù)據(jù)都大(索引默認(rèn)是降序排列,小→大),所以NULL值總是排在最后。 

使用方法: 
SQL> select 1 from dual where null=null; 

沒(méi)有查到記錄 

SQL> select 1 from dual where null=''''; 

沒(méi)有查到記錄 

SQL> select 1 from dual where ''''=''''; 

沒(méi)有查到記錄 

SQL> select 1 from dual where null is null; 


--------- 

SQL> select 1 from dual where nvl(null,0)=nvl(null,0); 


--------- 

對(duì)空值做加、減、乘、除等運(yùn)算操作,結(jié)果仍為空。 
SQL> select 1+null from dual; 
SQL> select 1-null from dual; 
SQL> select 1*null from dual; 
SQL> select 1/null from dual; 

查詢(xún)到一個(gè)記錄. 

注:這個(gè)記錄就是SQL語(yǔ)句中的那個(gè)null 

設(shè)置某些列為空值 
update table1 set 列1=NULL where 列1 is not null; 

現(xiàn)有一個(gè)商品銷(xiāo)售表sale,表結(jié)構(gòu)為: 
month    char(6)      --月份 
sell    number(10,2)   --月銷(xiāo)售金額 

create table sale (month char(6),sell number); 
insert into sale values(''200001'',1000); 
insert into sale values(''200002'',1100); 
insert into sale values(''200003'',1200); 
insert into sale values(''200004'',1300); 
insert into sale values(''200005'',1400); 
insert into sale values(''200006'',1500); 
insert into sale values(''200007'',1600); 
insert into sale values(''200101'',1100); 
insert into sale values(''200202'',1200); 
insert into sale values(''200301'',1300); 
insert into sale values(''200008'',1000); 
insert into sale(month) values(''200009'');(注意:這條記錄的sell值為空) 
commit; 
共輸入12條記錄 

SQL> select * from sale where sell like ''%''; 

MONTH SELL 
------ --------- 
200001 1000 
200002 1100 
200003 1200 
200004 1300 
200005 1400 
200006 1500 
200007 1600 
200101 1100 
200202 1200 
200301 1300 
200008 1000 

查詢(xún)到11記錄. 

結(jié)果說(shuō)明: 
查詢(xún)結(jié)果說(shuō)明此SQL語(yǔ)句查詢(xún)不出列值為NULL的字段 
此時(shí)需對(duì)字段為NULL的情況另外處理。 
SQL> select * from sale where sell like ''%'' or sell is null; 
SQL> select * from sale where nvl(sell,0) like ''%''; 

MONTH SELL 
------ --------- 
200001 1000 
200002 1100 
200003 1200 
200004 1300 
200005 1400 
200006 1500 
200007 1600 
200101 1100 
200202 1200 
200301 1300 
200008 1000 
200009 

查詢(xún)到12記錄. 

Oracle的空值就是這么的用法,我們最好熟悉它的約定,以防查出的結(jié)果不正確。 

但對(duì)于char 和varchar2類(lèi)型的數(shù)據(jù)庫(kù)字段中的null和空字符串是否有區(qū)別呢?

作一個(gè)測(cè)試:

create table test (a char(5),b char(5));

SQL> insert into test(a,b) values(''1'',''1'');

SQL> insert into test(a,b) values(''2'',''2'');

SQL> insert into test(a,b) values(''3'','''');--按照上面的解釋?zhuān)琤字段有值的

SQL> insert into test(a) values(''4'');

SQL> select * from test;

A B
---------- ----------
1 1
2 2
3
4

SQL> select * from test where b='''';----按照上面的解釋?zhuān)瑧?yīng)該有一條記錄,但實(shí)際上沒(méi)有記錄

未選定行

SQL> select * from test where b is null;----按照上面的解釋?zhuān)瑧?yīng)該有一跳記錄,但實(shí)際上有兩條記錄。

A B
---------- ----------
3
4

SQL>update table test set b='''' where a=''2'';
SQL> select * from test where b='''';

未選定行

SQL> select * from test where b is null;

A B
---------- ----------
2
3
4

上述內(nèi)容就是Oracle中Null與空字符串的區(qū)別是什么,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI