深入理解null的原理

--null的原理
--oracle一直将null和空字符串’’<长度为0>同等对待<如’’ is null是true,’’=null为false,如果声明a varchar2:=’’,那么a is null为true,a=’’为false>

--1.null的运算
--算术表达式和null 运算总为null,实际上所有的操作符除了||连接操作符外,只要有一个操作符为null,则结果为null。
--------------------------------null操作符运算------------------------------
--算术操作,只要有一个操作数为null,则结果为null
select null+10,null*10,null-10,null/10 from dual;
--连接操作符||除外,null相当于空字符串
select null||'abc', ''||'abc' from dual;--abc ,abc

--2.null在函数中的使用
create table nulltest as
select null a from dual
union all
select 1 from dual
union all
select 2 from dual;
alter table test2 add constraints uk_nulltest_id primary key(city);

--null在组函数中的使用,sum,avg,min,max都会忽略null值
select sum(a) from nulltest; --3
select avg(a) from nulltest; --1.5
select min(a) from nulltest; --1
select max(a) from nulltest; --2

--null在count函数中
--(1)如果指定count(*)或count(1)则不会忽略null(count(*)和count(1)效果一样,效率也没有什么差别)
--(2)如果是count(列名)则会忽略null
select count(*) from nulltest;     --3 包含了null的计算
select count(1) from nulltest;     --3 包含了null的计算
select count(a) from nulltest;     --2 忽略了null
select count(rowid) from nulltest; --3

--3.null在条件中的使用
--如果一个操作数含有null,只能通过is null 和is not null 来比较才能返回true 或false,否则返回的结果只能是unkown.

--4.null和索引、执行计划的关系(null在查询优化中的使用)
--如果是b*tree索引,单列索引,索引列上有null值,则is null不走索引,当然is not null可能走索引,看cbo计划.
--另外如果要走索引,可以使用bitmap索引或者使用复合索引,确保另一列上无null。
原文地址:https://www.cnblogs.com/huangbiquan/p/8001743.html