关于null值的小知识

关于null值的小知识
数据库中的逻辑关系是三元的,true,false,null。null 可以理解为神秘莫测的,不知道的,无法确定的。 今天要一层一层的剥去null 值的神秘外衣。。。额,貌似这样有些猥琐。言归正传下面来梳理下关于null 的知识。
一、null 值参与算数运算的结果为null,不过对于字符串的连接运算就不同了。
SQL> select 5+2-3 as result1,
  2         5+2-3+null as result2,
  3         3*5*null as result3
  4  from dual;
   RESULT1    RESULT2    RESULT3
---------- ---------- ----------
         4
SQL> select 'hello'||null||' oracle' from dual;
'HELLO'||NUL
------------
hello oracle
二、在聚集函数中avg,sum,min,max 等在求值的时候会忽略null值,但是count(*)  不会忽略null 值。 先造出一个空值来count(cloumn_name) 也是会忽略null 值的。
SQL> update employees
  2  set salary = null
  3  where employee_id = 100;
1 row updated.
SQL> select count(*) as row_count,count(salary),
  2  avg(salary) as avg_salary,sum(salary) as sum_salary,
  3  min(salary) as min_salary,max(salary) as max_salary
  4  from employees;
 ROW_COUNT COUNT(SALARY) AVG_SALARY SUM_SALARY MIN_SALARY MAX_SALARY
---------- ------------- ---------- ---------- ---------- ----------
       107           106 6296.22642     667400       2100      17000
三、在where 子句中使用null值。在where 中不能使用等于号指定null 值条件。应该使用is null 或者is not null.下面查询salary 值为null 的员工的姓名。
SQL> select last_name,first_name     
  2  from employees
  3  where salary = null;
no rows selected
SQL> select last_name,first_name
  2  from employees
  3  where salary is null;
LAST_NAME                 FIRST_NAME
------------------------- --------------------
King                      Steven
四、group by 子句中的null 值。先创建一个测试用的表testing02.
在group by 子句中不会忽略null 值,也会对null 进行分组。
SQL> create table testing02 (id number(6),name varchar2(20));
Table created.
SQL> insert into testing02 values(100,'testing');
1 row created.
SQL> insert into testing02 values(101,null);
1 row created.
SQL> commit;
Commit complet
SQL> select count(*) as row_count,name
  2  from testing02
  3  group by name;
 ROW_COUNT NAME
---------- --------------------
         1
         1 testing
五、在order by 中的null 值。通过在order by 中指定nulls first,nulls last 来 实现让带有null 值的列排在前面或者后面,不要太依赖select 语句查询的默认输出。
SQL> select * from testing02;
        ID NAME
---------- --------------------
       100 testing
       101
SQL> select * from testing02
  2  order by name nulls first;
        ID NAME
---------- --------------------
       101
       100 testing
SQL> select * from testing02
  2  order by name nulls last;
        ID NAME
---------- --------------------
       100 testing
       101
六、在聚合运算union,union all,minus,intersect  中不会忽略null 值 null 值将作为 相等的值来对待 此时可以认为null 等于 null.
7、null 值的插入。往表中的某一列插入一个null 值可以使用很多的方法,很多时候这 取决于习惯。
SQL> insert into testing02 values(102,'');
1 row created.
SQL> insert into testing02 values(103,null);
1 row created.
SQL> insert into testing02 (id) values(103);
1 row created.
要注意的是下面这条语句不会插入null 值,而是插入字符串null.
SQL> insert into testing02 values(102,'null');
1 row created.
SQL> commit;
Commit complete.
SQL> select * from testing02 
  2  where id > 101;
        ID NAME
---------- --------------------
       102
       103
       103
       102 null


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26110315/viewspace-722162/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/26110315/viewspace-722162/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值