3,Null详解
一行的一个cell如果没有值,那么就是null的,注意并不是他的值是null,所以col=null与col is null是完全不同的,null有很多特性,可以简单看几个例子来说明:
SQL> insert into t_test_chr values ('1','',null);
1 row created.
SQL> select * from t_test_chr where type_char=1;
TYPE TYPE_VARCHAR2 TYPE_NVARCHAR2
---- -------------------- --------------------
1
SQL> select * from t_test_chr where type_varchar2 is null and type_nvarchar2 is null;
TYPE TYPE_VARCHAR2 TYPE_NVARCHAR2
---- -------------------- --------------------
1
可以看出,''与null都是没有值的,可以认为他们相同,但是:
SQL> select * from dual where ''=null;
no rows selected
SQL> select * from dual where '' is null;
D
-
X
同样的情况:
SQL> select * from dual where '' ='';
no rows selected
SQL> select * from dual where null =null;
no rows selected
SQL> select * from dual where 1!=null;
no rows selected
这说明null不等于null,其他任何值与null的比较,结果是未知的。
在一些关联问题中字段包含了null值,通常情况下可以使用外连接来解决null值的影响。
同时也可以利用null的特性来实现一些特定的需求,比如:
SQL> create table t_test_null(id number,send_time date ,recv_time date );
Table created.
SQL> insert into t_test_null
2 select 1, sysdate, '' from dual;
1 row created.
SQL> insert into t_test_null
2 select 1, '', sysdate from dual;
1 row created.
插入的两条数据如果是模拟某种成对的操作,现在要知道某个id下面的send和recv时间,并且在一条中显示(往往情况比这个复杂),那么就可以使用:
SQL> select id ,min(send_time),min(recv_time) from t_test_null group by id;
ID MIN(SEND_ MIN(RECV_
---------- --------- ---------
1 16-MAY-10 16-MAY-10
或者使用max也可以,因为null的“值”比其他都小,或者都大。
一般为了消除控制字段中的null值出现,可以增加一个 not null约束,增加not null约束之后新插入或者更新后的字段值不允许为null,但是要注意的是,这个约束对增加之前已有的数据不会检查。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/16179598/viewspace-662798/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/16179598/viewspace-662798/