博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
oracle中的null详解
阅读量:7223 次
发布时间:2019-06-29

本文共 2082 字,大约阅读时间需要 6 分钟。

hot3.png

在不同的数据库中null是有可能是不同的。在oracle中,含有空值的表列长度为0。oracle允许任何一种数据类型的字段为空,除非以下两种情况: 1、主键字段(primary key) 2、定义时已经加了NOT NULL 限制条件的字段 说明: 1、等价于没有任何值、是未知数。 2、NULL 与0、空字符串、空格都不同 3、对空值做加减乘除等运算操作,结果仍为空 4、NULL 的处理使用NVL函数 5、比较时是使用关键字“is null”和“is not null” 6、空值不能被索引,所以查询时有些字符和条件的数据可能会查询不出来count(*)中,用nvl(列名,0)处理后再查 7、排序时比其他数据都大(索引默认是降序排列,小→大), 所以NULL值总是排在最后

使用方法: SQL> select 1 from emp where null =null; 没有查到记录 SQL> select 1 from emp where null =""; 没有查询到记录 SQL> select 1 from emp where ""=""; 没有查询到记录 SQL> select 1 from emp where null is null; 1 ----------

1 SQL> select 1 from emp where nvl(null,0)=nvl(null,0); 1 ----------
1

对空值做加、减、乘、除等运算操作,结果仍为空。 SQL> select 1+null from emp; SQL> select 1-null from emp; SQL> select 1*null from emp; SQL> select 1/null from emp; 查询到一个记录,就是SQL语句中的那个null。

设置某些列为空值 update table1 set 列1=NULL where 列1 is not null; 现有一个商品销售表sale,表结构为: month    char(6)      --月份 sell    number(10,2)   --月销售金额

create table sale (month char(6),sell number); insert into sale values('200001',1000); insert into sale values('200002',1100); insert into sale values('200003',1200); insert into sale values('200004',1300); insert into sale values('200005',1400); insert into sale values('200006',1500); insert into sale values('200007',1600); insert into sale values('200101',1100); insert into sale values('200202',1200); insert into sale values('200301',1300); insert into sale values('200008',1000); insert into sale(month) values('200009'); (注意:这条记录的sell值为空) commit; 共输入12条记录 SQL> select * from sale where sell like '%'; MONTH SELL


200001 1000 200002 1100 200003 1200 200004 1300 200005 1400 200006 1500 200007 1600 200101 1100 200202 1200 200301 1300 200008 1000

查询到11记录.

结果说明: 查询结果说明此SQL语句查询不出列值为NULL的字段 此时需对字段为NULL的情况另外处理。

SQL> select * from sale where sell like '%' or sell is null; SQL> select * from sale where nvl(sell,0) like '%';

MONTH SELL


200001 1000 200002 1100 200003 1200 200004 1300 200005 1400 200006 1500 200007 1600 200101 1100 200202 1200 200301 1300 200008 1000 200009

查询到12记录.

Oracle的空值就是这么的用法,我们最好熟悉它的约定,以防查出的结果不正确

转载于:https://my.oschina.net/lzwenme/blog/223053

你可能感兴趣的文章
js实现60s倒计时效果
查看>>
【POJ 2176】Folding
查看>>
redis的过期策略以及内存淘汰机制
查看>>
阿牛的EOF牛肉串
查看>>
随笔2013/2/13
查看>>
笨办法32循环和列表
查看>>
java序列化
查看>>
谈谈NITE 2的第一个程序HandViewer
查看>>
VS2008 未响应 假死
查看>>
html5、css3及响应式设计入门
查看>>
Win10還原成最乾淨的狀態
查看>>
Java_InvokeAll_又返回值_多个线程同时执行,取消超时线程
查看>>
SaltStack作业
查看>>
单例设计
查看>>
springboot+缓存
查看>>
/*10个filter的属性*/ ---毛玻璃效果
查看>>
折半查找习题解答
查看>>
51单片机的P1
查看>>
[32]JSON
查看>>
3689: 异或之
查看>>