Oracle中Null與Null 字元串' '的區別

來源:互聯網
上載者:User

標籤:

含義解釋:
問:什麼是NULL?
答:在我們不知道具體有什麼資料的時候,也即未知,可以用NULL,我們稱它為空白,ORACLE中,含有空值的表列長度為零。
ORACLE允許任何一種資料類型的欄位為空白,除了以下兩種情況:
1、主鍵欄位(primary key),
2、定義時已經加了NOT NULL限制條件的欄位
說明:
1、等價於沒有任何值、是未知數。
2、NULL與0、Null 字元串、空格都不同。
3、對空值做加、減、乘、除等運算操作,結果仍為空白。
4、NULL的處理使用NVL函數。
5、比較時使用關鍵字用“is null”和“is not null”。
6、空值不能被索引,所以查詢時有些合格資料可能查不出來,count(*)中,用nvl(列名,0)處理後再查。
7、排序時比其他資料都大(索引預設是降序排列,小→大),所以NULL值總是排在最後。
使用方法:
SQL> select 1 from dual where null=null;
沒有查到記錄
SQL> select 1 from dual where null=‘‘;
沒有查到記錄
SQL> select 1 from dual where ‘‘=‘‘;
沒有查到記錄
SQL> select 1 from dual where null is null;
1
---------
1

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

1
---------
1
對空值做加、減、乘、除等運算操作,結果仍為空白。
SQL> select 1+null from dual;
SQL> select 1-null from dual;
SQL> select 1*null from dual;
SQL> select 1/null from dual;
查詢到一個記錄.
註:這個記錄就是SQL語句中的那個null
設定某些列為空白值
update table1 set 列1=NULL where 列1 is not null;
現有一個商品銷售表sale,表結構為:
month    char(6)      --月份
sell    number(10,2)   --月銷售金額
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
查詢到11記錄.
結果說明:
查詢結果說明此SQL語句查詢不出列值為NULL的欄位
此時需對欄位為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
查詢到12記錄.
Oracle的空值就是這麼的用法,我們最好熟悉它的約定,以防查出的結果不正確。

但對於char 和varchar2類型的資料庫欄位中的null和Null 字元串是否有區別呢?
作一個測試:
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‘,‘‘);--按照上面的解釋,b欄位有值的
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=‘‘;----按照上面的解釋,應該有一條記錄,但實際上沒有記錄
未選定行
SQL> select * from test where b is null;----按照上面的解釋,應該有一跳記錄,但實際上有兩條記錄。
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
測試結果說明,對char和varchar2欄位來說,‘‘就是null;但對於where 條件後的‘‘ 不是null。
對於預設值,也是一樣的!

Oracle中Null與Null 字元串' '的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.