zoukankan      html  css  js  c++  java
  • 关于null值

    关于null值的小知识
    数据库中的逻辑关系是三元的,true,false,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
  • 相关阅读:
    一分钟教你解决前端分流问题
    win7 mysql 数据库轻松实现数据库定时备份
    mysql 修改密码
    what is yaml ?
    php实现监控在线服务应用程序小栗子
    Python装饰器小代码
    2,构造代码块
    1,匿名对象,封装
    7,random
    测试错题
  • 原文地址:https://www.cnblogs.com/zhaoxr/p/2672208.html
Copyright © 2011-2022 走看看