您的位置:首页 > 数据库 > Oracle

oracle is null 和is not null

2012-10-31 20:58 148 查看

当字符串为空时,在where 使用 is null来判断。


例如:

create table test

(

stuno number(10) primary key,

stuname varchar2(50)

)

insert into test(stuno) values(10)

insert into test(stuno,stuname) values(11,' ')

select * from test where stuname is null才会取到所有值。

而select * from test where stuname=''不行。

oracle中的null处理的简单理解

首先对oracle中的null做一些简单的说明:

1、oracle中null与0,空字符串,空格,包括null与null也是不等的。

2、oracle中对null做算术运算结果仍然为null。

3、处理方法有nvl函数,比较方法是is null或者is not null。

4、null不能被索引,比如select count(null) from dual的查询结果是0。

5、null排序比其他所有类型的大,一个可以为空的字段倒排序,前面的都是null数据。

下面举例说明:

1、

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

未选定行

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

未选定行

SQL> select 1 from dual where
null=0; false

未选定行

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

未选定行

以上4个查询中,大家可以看到null在oracle中可以理解为什么都没有的意思,它与任何对象都是不相等的。

2、

SQL> select 1+null from dual;

1+NULL

----------

SQL> select 1-null from dual;

1-NULL

----------

SQL> select 1*null from dual;

1*NULL

----------

SQL> select 1/null from dual;

1/NULL

---------

-

SQL> select 1 from dual where 1+null is null;

1

----------

1

SQL> select 1 from dual where 1-null is null;

1

----------

1

SQL> select 1 from dual where 1*null is null;

1

----------

1

SQL> select 1 from dual where 1/null is null;

1

----------

1

以上是null的加减乘除操作,可见null的加减乘除是null。

3、

SQL> select 1 from dual where null is null;

1

----------

1

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

1

----------

1

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

1

----------

1

以上两个查询可以看出null的判断方法是is null,nvl是对null做处理的函数;而且,空字符串也被认为是null

提示:null是不能被%匹配到的如select 1 from dual where null like '%'是查不到结果的。

4、

SQL> select count(null) from dual;

COUNT(NULL)

-----------

0

SQL> select count(nvl(null, 0)) from dual;

COUNT(NVL(NULL,0))

------------------

1

以上两个查询提醒大家在统计的时候注意根据自己的需要处理null字段的问题。

其他说明:

根据oracle中null的定义,null为未知,不等于null,因此在表中的唯一字段中存在多个null是不会违反唯一行约束的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: