ORACLE空值漫谈1

    技术2026-03-30  8

    诺网 Oracle 新闻频道 开发频道 系统频道 服务器 网络频道 网络安全 Java频道 C/C++ PHP开发 电子书 资源下载 社 区 博 客 在线试题 数据库 Access Mysql Mssql Oracle Sybase FoxPro db2 数据库相关文章 Oracle系列教程 Oracle技术教程 Oracle实例教程 编程开发 JAVA C/C++ C++ VC C语言 VB C# Delphi Foxpro 汇编 shell编程 游戏开发 软件工程师 WEB开发 PHP ASP Asp.net JSP AJAX CGI JavaScript HTML CSS 数据库 MSSQL Mysql Oracle Access Sybase DB2 sql2005 Office Word Excel Powerpoint Wps 认证考试 二级C语言 三级网络 程序员 网络工程师 思科认证  您当前的位置:飞诺网 >>  数据库 >>  Oracle >> Oracle技术教程oracle空值null的漫谈www.firnow.com    时间 : 2008-02-24  作者:佚名   编辑:本站 点击:  468 [ 评论 ]--在数据库中,空值用来表示实际值未知或无意义的情况。在一个表中,如果一行中的某列没有值,那么就称它为空值(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 207499 allen salesman 7698 20-feb-81 1600 300 307521 ward salesman 7698 22-feb-81 1250 500 307566 jones manager 7839 02-apr-81 2975 207654 martin salesman 7698 28-sep-81 1250 1400 307698 blake manager 7839 01-may-81 2850 307782 clark manager 7839 09-jun-81 2450 107788 scott analyst 7566 09-dec-82 3000 207839 king president 17-nov-81 5000 107844 turner salesman 7698 08-sep-81 1500 0 307876 adams clerk 7788 12-jan-83 1100 207900 james clerk 7698 03-dec-81 950 307902 ford analyst 7566 03-dec-81 3000 207934 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 selectedsql>select * from emp where mgr=null; no rows selectedsql>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.空值和逻辑操作符

    逻辑操作符表达式结果andnull and truenull

    null and falsefalse

    null and nullnullornull or truetrue

    null or falsenull

    null or nullnullnotnot nullnull

    可以看到,在真值表中,除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 selectedsql> 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 300ward 1250 500turner 1500 0sal或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,somesql>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)allsql>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)betweensql>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 nulla is nulltrue

    a is not nullfalse

    c is nullfalse

    c is not nulltrue=、!=、>=、<=、>、<a = nullnull

    a > nullnull

    c = nullnull

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

    c in (10,null)true

    c in (20,null)nullnot in (等价于!=all)a not in (20,null)null

    c not in (20,null)false

    c not in (10,null)nullany,somea > any(5,null)null

    c > any(5,null)true

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

    c > all(5,null)null

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

    c between 5 and nullnull

    c between 15 and nullfalse

    a not between 5 and nullnull

    c not between 5 and nullnull

    c not between 15 and nulltrue

    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 1900ward 7698 ward7698 1250 500 1750jones 7839 jones7839 2975 martin 7698 martin7698 1250 1400 2650blake 7839 blake7839 2850 clark 7839 clark7839 2450 scott 7566 scott7566 3000 king king 5000 turner 7698 turner7698 1500 0 1500adams 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 w

    文章出处:飞诺网(www.firnow.com):http://dev.firnow.com/course/7_databases/oracle/oraclejs/2008224/100970.html

    最新回复(0)