程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> Oracle數據庫 >> Oracle數據庫基礎 >> oracle空值null的漫談

oracle空值null的漫談

編輯:Oracle數據庫基礎
數據庫中,空值用來表示實際值未知或無意義的情況。在一個表中,如果一行中的某列沒有值,那麼就稱它為空值(null)。任何數據類型的列,只要沒有使用非空(not null)或主鍵(primary key)完整性限制,都可以出現空值。在實際應用中,如果忽略空值的存在,將會造成造成不必要的麻煩。

例如,在下面的雇員表(emp)中,雇員名(ename)為king的行,因為king為最高官員(president),他沒有主管(mgr),所以其mgr為空值。因為不是所有的雇員都有手續費(comm),所以列comm允許有空值,除300、500、1400、0以外的其它各行comm均為空值。
empno ename job mgr hiredate sal comm deptno
--------- ---------- --------- --------- --------- --------- --------- ---------
7369 smith clerk 7902 17-dec-80 800 20
7499 allen salesman 7698 20-feb-81 1600 300 30
7521 ward salesman 7698 22-feb-81 1250 500 30
7566 jones manager 7839 02-apr-81 2975 20
7654 martin salesman 7698 28-sep-81 1250 1400 30
7698 blake manager 7839 01-may-81 2850 30
7782 clark manager 7839 09-jun-81 2450 10
7788 scott analyst 7566 09-dec-82 3000 20
7839 king president 17-nov-81 5000 10
7844 turner salesman 7698 08-sep-81 1500 0 30
7876 adams clerk 7788 12-jan-83 1100 20
7900 james clerk 7698 03-dec-81 950 30
7902 ford analyst 7566 03-dec-81 3000 20
7934 miller clerk 7782 23-jan-82 1300 10

本文將以上述emp表為例,具體討論一下空值在日常應用中所具有的一些特性。

一、空值的生成及特點

1. 空值的生成

如果一列沒有非空(not null)完整性限制,那麼其缺省的值為空值,即如果插入一行時未指定該列的值,則其值為空值。

使用sql語句insert插入行,凡未涉及到的列,其值為空值;涉及到的列,如果其值確實為空值,插入時可以用null來表示(對於字符型的列,也可以用''''來表示)。

例:插入一行,其empno為1、ename為''jia''、sal為10000、job和comm為空值。
sql>insert into emp(empno,ename,job,sal,comm) values(1,''jia'',null,1000,null);
sql>select * from emp where empno=1;
empno ename job mgr hiredate sal comm deptno
--------- ---------- --------- --------- --------- --------- --------- ---------
1 jia 1000

可以看到新插入的一行,除job和comm為空值外,mgr、hiredate、deptno三列由於插入時未涉及,也為空值。

使用sql語句update來修改數據,空值可用null來表示(對於字符型的列,也可以用''''來表示)。例:
sql>update emp set ename=null,sal=null where empno=1;

2. 空值的特點

空值具有以下特點:

* 等價於沒有任何值。
* 與 0、空字符串或空格不同。
* 在where條件中, Oracle認為結果為null的條件為false,帶有這樣條件的select語句不返回行,並且不返回錯誤信息。但null和false是不同的。
* 排序時比其他數據都大。
* 空值不能被索引。

二、空值的測試

因為空值表示缺少數據,所以空值和其它值沒有可比性,即不能用等於、不等於、大於或小於和其它數值比較,當然也包括空值本身(但是在decode中例外,兩個空值被認為是等價)。測試空值只能用比較操作符is null 和is not null。如果使用帶有其它比較操作符的條件表達式,並且其結果依賴於空值,那麼其結果必定是null。在where條件中,Oracle認為結果為null的條件為false,帶有這樣條件的select語句不返回行,也不返回錯誤信息。

例如查詢emp表中mgr為null的行:
sql>select * from emp where mgr='''';
no rows selected
sql>select * from emp where mgr=null;
no rows selected
sql>select * from emp where mgr is null;
empno ename job mgr hiredate sal comm deptno
--------- ---------- --------- --------- --------- --------- --------- ---------
7839 king president 17-nov-81 5000 10
第1、2句寫法不妥,where條件結果為null,不返回行。第三句正確,返回mgr為空值的行。
三、 空值和操作符

1.空值和邏輯操作符

邏輯操作符
表達式
結果
and
null and true
null

null and false
false

null and null
null
or
null or true
true

null or false
null

null or null
null
not
not null
null

可以看到,在真值表中,除null and false 結果為false、null or true結果為true以外,其它結果均為null。

雖然在where條件中,Oracle認為結果為null的where條件為false,但在條件表達式中null不同於false。例如在not ( null and false )和not ( null and null )二者中僅有一處false和true的區別,但not ( null and false )的結果為 true,而not ( null and null )的結果為null。

下面舉例說明空值和邏輯操作符的用法:

sql> select * from emp where not comm=null and comm!=0;
no rows selected
sql> select * from emp where not ( not comm=null and comm!=0 );
empno ename job mgr hiredate sal comm deptno
--------- ---------- --------- --------- --------- --------- --------- ---------
7844 turner salesman 7698 08-sep-81 1500 0 30

第一個select語句,條件“not comm=null and comm!=0”等價於null and comm!=0。對於任意一行,如果comm為不等於0的數值,條件等價於null and true,結果為null;如果comm等於0,條件等價於null and false,結果為false。所以,最終結果不返回行。

第二個select語句的條件為第一個select語句條件的“非”(not),對於任意一行,如果comm為不等於0的數值,條件等價於not null,結果為null;如果comm等於0,條件等價於not false,結果為true。所以,最終結果返回行comm等於0的行。

2.空值和比較操作符

(1)is [not] null:是用來測試空值的唯一操作符(見“空值的測試”)。
(2)=、!=、>=、<=、>、<
sql>select ename,sal,comm from emp where sal>comm;
ename sal comm
---------- --------- ---------
allen 1600 300
ward 1250 500
turner 1500 0
sal或comm為空值的行,sal>comm比較結果為null,所以凡是sal或comm為空值的行都沒有返回。
(3)in和not in操作符
sql>select ename,mgr from emp where mgr in (7902,null);
ename mgr
---------- ---------
smith 7902

在上述語句中,條件“mgr in (7902,null)”等價於mgr=7902 or mgr=null。

對於表emp中的任意一行,如果mgr為null,則上述條件等價於null or null,即為null;如果mgr為不等於7902的數值,則上述條件等價於false or null,即為null;如果mgr等於7902,則上述條件等價於true or null,即為true。所以,最終結果能返回mgr等於7902的行。

sql>select deptno from emp where deptno not in (''10'',null);
no rows selected
在上述語句中,條件“deptno not in (''10'',null)”等價於deptno!=''10'' and deptno!=null,對於emp表中的任意一行,條件的結果只能為null或false,所以不返回行。

(4)any,some
sql>select ename,sal from emp where sal> any(3000,null);
ename sal
---------- ---------
king 5000
條件“sal> any(3000,null)”等價於sal>3000 or sal>null。類似前述(3)第一句,最終結果返回所有sal>3000的行。

(5)all
sql>select ename,sal from emp where sal> all(3000,null);
no rows selected
條件“sal> all(3000,null)”等價於sal>3000 and sal>null, 結果只能為null或false,所以不返回行。

(6)(not)between
sql>select ename,sal from emp where sal between null and 3000;
no rows selected
條件“sal between null and 3000”等價於sal>=null and sal<=3000, 結果只能為null或false,所以不返回行。
sql>select ename,sal from emp where sal not between null and 3000;
ename sal
---------- ---------
king 5000
條件“sal not between null and 3000”等價於sal<null or sal>3000,類似前述(3)的第一句,結果返回sal>3000的行。
下表為比較操作符和空值的小結:
比較操作符
表達式(例:a、b是null、c=10)
結果
is null、is not null
a is null
true

a is not null
false

c is null
false

c is not null
true
=、!=、>=、<=、>、<
a = null
null

a > null
null

c = null
null

c > null
null
in (=any)
a in (10,null)
null

c in (10,null)
true

c in (20,null)
null
not in
(等價於!=all)
a not in (20,null)
null

c not in (20,null)
false

c not in (10,null)
null
any,some
a > any(5,null)
null

c > any(5,null)
true

c > any(15,null)
null
all
a > all(5,null)
null

c > all(5,null)
null

c > all(15,null)
false
(not)between
a between 5 and null
null

c between 5 and null
null

c between 15 and null
false

a not between 5 and null
null

c not between 5 and null
nullc not between 15 and null
true

3、 空值和算術、字符操作符

(1)算術操作符:空值不等價於0,任何含有空值的算術表達式其運算結果都為空值,例如空值加10為空值。

(2)字符操作符:因為Oracle目前處理零個字符值的方法與處理空值的方法相同(日後的版本中不一定仍然如此),所以對於,空值等價於零個字符值。例:
sql>select ename,mgr,enamemgr,sal,comm,sal+comm from emp;
ename mgr enamemgr sal comm sal+comm
---------- --------- ------------- --------- --------- ---------
smith 7902 smith7902 800
allen 7698 allen7698 1600 300 1900
ward 7698 ward7698 1250 500 1750
jones 7839 jones7839 2975
martin 7698 martin7698 1250 1400 2650
blake 7839 blake7839 2850
clark 7839 clark7839 2450
scott 7566 scott7566 3000
king king 5000
turner 7698 turner7698 1500 0 1500
adams 7788 adams7788 1100
james 7698 james7698 950
ford 7566 ford7566 3000
miller 7782 miller7782 1300
我們可以看到,凡mgr為空值的,enamemgr結果等於ename;凡是comm為空值的行,sal+comm均為空值。

四、空值和函數

1.空值和度量函數

對於度量函數,如果給定的參數為空值,則其(nvl、translate除外)返回值為空值。如下例中的abs(comm),如果comm為空值,abs(comm)為空值。
sql> select ename,sal,comm,abs(comm) from emp where sal<1500;
ename sal comm abs(comm)
---------- --------- --------- ---------
smith 800
ward 1250 500 500
martin 1250 1400 1400
adams 1100
james 950
miller 1300

2.空值和組函數

組函數忽略空值。在實際應用中,根據需要可利用nvl函數用零代替空值。例:
sql>select count(comm),sum(comm),avg(comm) from emp;
count(comm) sum(comm) avg(comm)
----------- --------- ---------
4 2200 550
sql>select count(nvl(comm,0)),sum(nvl(comm,0)),avg(nvl(comm,0))
from emp;
count(nvl(comm,0)) sum(nvl(comm,0)) avg(nvl(comm,0))
------------------ ---------------- ----------------
14 2200 157.14286

第一個select語句忽略comm為空值的行,第二個select語句使用nvl函數統計了所有的comm,所以它們統計的個數、平均值都不相同。

另外需要注意的是,在利用組函數進行數據處理時,不同的寫法具有不同的不同含義,在實際應用中應靈活掌握。例如:
sql>select deptno,sum(sal),sum(comm), sum(sal+comm),sum(sal)+sum(comm),sum(nvl(sal,0)+nvl(comm,0))
from emp
group by deptno;
deptno sum(sal) sum(comm) sum(sal+comm) sum(sal)+sum(comm) sum(nvl(sal,0)+nvl(comm,0))
--------- --------- --------- ------------- ------------------ ---------------------------
10 8750 8750
20 10875 10875
30 9400 2200 7800 11600 11600

可以看到sum(sal+comm)、sum(sal)+sum(comm)、 sum(nvl(sal,0)+nvl(comm,0))的區別:sum(sal+comm)為先加然後計算各行的和,如果sal、comm中有一個為null,則該行忽略不計;sum(sal)+sum(comm)為先計算各行的合計然後再加,sal、comm中的null都忽略不計,但如果 sum(sal)、sum(comm)二者的結果之中有一個為null,則二者之和為null;在sum(nvl(sal,0)+nvl(comm,0))裡,sal、comm中的null按0處理。

五、空值的其它特性

1.空值在排序時大於任何值。例如:
sql> select ename,comm from emp where deptno=''30'' order by comm;
ename comm
---------- ---------
turner 0
allen 300
ward 500
martin 1400
blake
james

2.空值不能被索引。雖然在某列上建立了索引,但是對該列的空值查詢來說,因為空值沒有被索引,所以不能改善查詢的效率。例如下面的查詢不能利用在mgr列上創建的索引。
sql>select ename from emp where mgr is null;
ename
----------
king

另外正是因為空值不被索引,所以可在含有空值的列上建立唯一性索引(unique index)。例如,可以在emp表的comm列上建立唯一性索引:
sql> create unique index emp_comm on emp(comm);
index created. 



  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved