NULL及DUAL详解

来源:互联网 发布:淘宝提前收款疯狂扣费 编辑:程序博客网 时间:2024/06/05 05:03
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

1NULL 使用详解

    常常会有人问到,什么是NULL?顾名思义,NULL就是空,ORACLE中以及其他的数据库中,含有空值的表的列的长度为零。ORACLE允许任何一种数据类型的字段为空,除了以下两种情况:

    1、定义该列为主键字段(primary key);

    2、定义该列时已显式的加了 NOT NULL 的限制条件的字段。

1.1.具体说明:

    1、等价于没有任何值、是未知数;

    2NULL0、空字符串、空格都不同;

    3、对空值做加、减、乘、除等运算操作,结果仍为空;

    4NULL的处理使用NVL函数;

    5、查询、比较时使用关键字用“is NULL”和“is not NULL”;

    6、空值不能被索引,所以查询时有些符合条件的数据可能查不出来,比方在count(*)中,用nvl(列名,0)处理后再查;

7、排序时比其他数据都大(索引默认是降序排列,小→大),所以NULL值总是排在最后。

 

1.2.使用方法举例:

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;

查询到一个记录.

1.3.设置某些列为空值

update table1 set col1=NULL where col1 is not NULL;

熟练使用Oracle的空值用法,熟悉它的约定,以确保查出的结果OK

2DUAL伪列

含义解释:

DUAL Oracle中的一个实际存在的表,任何用户均可读取,常用在没有目标表的Select语句块中。

2.1.使用方法:

--查看当前连接用户

SQL> select user from DUAL;

USER

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

SYSTEM

--查看当前日期、时间

SQL> select sysdate from DUAL;

SYSDATE

----------

18-4-03

SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from DUAL;

TO_CHAR(SYSDATE,'YY

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

2003-04-18 22:37:56

  --当作计算器用

SQL> select 1+2 from DUAL;

       1+2

----------

         3

--查看序列值

SQL> create sequence aaa increment by 1 start with 1;

SQL> select aaa.nextval from DUAL;

          NEXTVAL

----------

         1

       SQL> select aaa.currval from DUAL;

 

   CURRVAL

----------

         1

 

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击