Oracle开发者中级第1课(Null)实验

Posted dingdingfish

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle开发者中级第1课(Null)实验相关的知识,希望对你有一定的参考价值。

概述

本实验参考DevGym中的实验指南

创建环境

首先创建表:

create table toys (
  toy_id               integer not null primary key,
  toy_name             varchar2(100) not null,
  weight               number(10, 2) not null,
  quantity_of_stuffing integer,
  volume_of_wood       integer,
  times_lost           integer
);

insert into toys values (1, 'Mr. Penguin', 50, 100, null, 10);
insert into toys values (2, 'Blue Brick', 10, null, 10, null);
insert into toys values (3, 'Red Brick', 20, null, 20, 1);
commit;

查看数据:

SQL> select * from toys;

   TOY_ID       TOY_NAME    WEIGHT    QUANTITY_OF_STUFFING    VOLUME_OF_WOOD    TIMES_LOST
_________ ______________ _________ _______________________ _________________ _____________
        1 Mr. Penguin           50                     100                              10
        2 Blue Brick            10                                        10
        3 Red Brick             20                                        20             1

Null不等于任何值

同时null也不会参与比较查询。

SQL> select * from toys
  2  where  volume_of_wood = null;

no rows selected

SQL> select * from toys
  2  where  volume_of_wood <> null;

no rows selected

SQL> select * from toys
  2  where  volume_of_wood < 15;

   TOY_ID      TOY_NAME    WEIGHT    QUANTITY_OF_STUFFING    VOLUME_OF_WOOD    TIMES_LOST
_________ _____________ _________ _______________________ _________________ _____________
        2 Blue Brick           10                                        10

IS (NOT) NULL

必须使用IS (NOT) NULL判断。

SQL> select *
  2  from   toys
  3  where  volume_of_wood is null;

   TOY_ID       TOY_NAME    WEIGHT    QUANTITY_OF_STUFFING    VOLUME_OF_WOOD    TIMES_LOST
_________ ______________ _________ _______________________ _________________ _____________
        1 Mr. Penguin           50                     100                              10

SQL> select *
  2  from   toys
  3  where  volume_of_wood is not null;

   TOY_ID      TOY_NAME    WEIGHT    QUANTITY_OF_STUFFING    VOLUME_OF_WOOD    TIMES_LOST
_________ _____________ _________ _______________________ _________________ _____________
        2 Blue Brick           10                                        10
        3 Red Brick            20                                        20             1

Null函数

包括NVLCoalesce

SQL> select toy_name, volume_of_wood, nvl ( volume_of_wood , 0 ) mapped_volume_of_wood
  2  from   toys;

      TOY_NAME    VOLUME_OF_WOOD    MAPPED_VOLUME_OF_WOOD
______________ _________________ ________________________
Mr. Penguin                                             0
Blue Brick                    10                       10
Red Brick                     20                       20

SQL> select t.*,
  2         coalesce ( volume_of_wood , 0 ) coalesce_two,
  3         coalesce ( times_lost, volume_of_wood , quantity_of_stuffing, 0 ) coalesce_many
  4  from   toys t;

   TOY_ID       TOY_NAME    WEIGHT    QUANTITY_OF_STUFFING    VOLUME_OF_WOOD    TIMES_LOST    COALESCE_TWO    COALESCE_MANY
_________ ______________ _________ _______________________ _________________ _____________ _______________ ________________
        1 Mr. Penguin           50                     100                              10               0               10
        2 Blue Brick            10                                        10                            10               10
        3 Red Brick             20                                        20             1              20                1

Null是你的SQL变得复杂,有时也会使用魔数(Magic Number)。作者的意思是不建议,我觉得也看情况,把含义沟通清楚即可。

以上是关于Oracle开发者中级第1课(Null)实验的主要内容,如果未能解决你的问题,请参考以下文章

Oracle开发者中级第5课(Pivot 和Unpivot)实验

Oracle开发者中级第8课(Merge)实验

Oracle开发者中级第8课(Merge)实验

Oracle开发者中级第2课(子查询)实验

Oracle开发者中级第7课(层级查询)实验

Oracle开发者中级第7课(层级查询)实验