ORACLE 학습 노트 시리즈 (8) ORACLE 의 빈 값 (NULL) 과 빈 값 (NULL) 과 관련 된 함수

5764 단어 oracle 학습
 ORACLE      (8)ORACLE    (NULL)    (NULL)     

 ORACLE                 ,    ,   NULL,    。
 ORACLE ,           。

 ORACLE               ,        :
    a、    (primary key)
    b、       NOT NULL       

   (NULL)     :
1、NULL        、    。
2、ORACLE NULL 0,    ,  ,  NULL NULL     。
3、 NULL   、 、 、      ,    NULL。
4、NULL      NVL(EXPR1,EXPR2)  。
5、         “IS NULL” “IS NOT NULL”。
6、       ,            ,NULL         ,COUNT(COLUMN1)    COLUMN1 NULL  。
7、  SUM(COLUMN1)  ,   COLUMN1 NULL  ,       。
8、          (         , → ),  NULL       。    “NULLS FIRST” “NULLS LAST”  NUUL    。
9、“IN”、“NOT IN”          。
10、  LIKE     ,NULL   %   。
11、ORACLE    (NULL)     。


--1
SELECT NULL, '', '  ', 0 FROM DUAL;

--2
SELECT 'NULL=NULL' AS T, COUNT(*) AS C FROM DUAL WHERE NULL = NULL;
SELECT 'NULL=''''' AS T, COUNT(*) AS C FROM DUAL WHERE NULL = '';
SELECT 'NULL=0' AS T, COUNT(*) AS C FROM DUAL WHERE NULL = 0;
SELECT '''''=''''' AS T, COUNT(*) AS C FROM DUAL WHERE '' = '';
SELECT 'a!=null' AS T, COUNT(*) AS C FROM DUAL WHERE 'a' != NULL;
SELECT 'a=a' AS T, COUNT(*) AS C FROM DUAL WHERE 'a' = 'a';
 
--3
SELECT 1 + NULL, 1 - NULL, 1 * NULL, 1 / NULL FROM DUAL;
SELECT '1 + NULL IS NULL' AS T, COUNT(*) AS C FROM DUAL WHERE 1 + NULL IS NULL;
SELECT '1 - NULL IS NULL' AS T, COUNT(*) AS C FROM DUAL WHERE 1 - NULL IS NULL;
SELECT '1 * NULL IS NULL' AS T, COUNT(*) AS C FROM DUAL WHERE 1 * NULL IS NULL;
SELECT '1 / NULL IS NULL' AS T, COUNT(*) AS C FROM DUAL WHERE 1 / NULL IS NULL;
  
--4
SELECT NVL(NULL, 1), NVL(NULL, 'a'), NVL(NULL, SYSDATE), NVL('a', 1) FROM DUAL;

--5
SELECT 'NULL IS NULL' AS T, COUNT(*) AS C FROM DUAL WHERE NULL IS NULL;
SELECT ''''' IS NULL' AS T, COUNT(*) AS C FROM DUAL WHERE '' IS NULL;
SELECT 'NVL IS NOT NULL' AS T, COUNT(*) AS C FROM DUAL WHERE NVL(NULL, 0) IS NOT NULL;

--6
SELECT COUNT(*), COUNT(1), COUNT(NULL), COUNT(NVL(NULL, 0)) FROM DUAL;
 
--7
SELECT COUNT(*), SUM(1), SUM(LEVEL), SUM(NVL(NULL, 0)) FROM DUAL CONNECT BY LEVEL <= 4;

--8
SELECT * FROM B ORDER BY NAME NULLS FIRST;
SELECT * FROM B ORDER BY NAME NULLS LAST;

--9
SELECT 'a=a' AS T, COUNT(*) AS C FROM DUAL WHERE 'a' = (NULL);
SELECT 'a=a' AS T, COUNT(*) AS C FROM DUAL WHERE 'a' IN (NULL);
SELECT 'a=a' AS T, COUNT(*) AS C FROM DUAL WHERE 'a' IN ('a', NULL);

SELECT 'a=a' AS T, COUNT(*) AS C FROM DUAL WHERE 'b' = (NULL);
SELECT 'a=a' AS T, COUNT(*) AS C FROM DUAL WHERE 'b' NOT IN (NULL);
SELECT 'a=a' AS T, COUNT(*) AS C FROM DUAL WHERE 'b' NOT IN ('a', NULL);

SELECT 'a=a' AS T, COUNT(*) AS C FROM DUAL WHERE 'a' = (SELECT NULL FROM DUAL);
SELECT 'a=a' AS T, COUNT(*) AS C FROM DUAL WHERE 'a' IN (SELECT NULL FROM DUAL);
SELECT 'a=a' AS T, COUNT(*) AS C FROM DUAL WHERE 'a' NOT IN (SELECT NULL FROM DUAL);

--10 
SELECT 'NULL LIKE %' AS T, COUNT(*) AS C FROM DUAL WHERE NULL LIKE '%';
 

--11 ORACLE    (NULL)     

(1)NVL
  :NVL(EXPR1,EXPR2) 
   : (CASE WHEN EXPR1 IS NULL THEN EXPR2 ELSE EXPR1 END)
  :NVL(EXPR1,EXPR2) EXPR1 NULL , EXPR2  ,   EXPR1  ;
    :    SELECT   WHERE   ;
          EXPR1,EXPR2          ;

(2)NVL2 
  :NVL2(EXPR1,EXPR2,EXPR3) 
   : (CASE WHEN EXPR1 IS NULL THEN EXPR3 ELSE EXPR2 END) 
  :NVL2(EXPR1,EXPR2,EXPR3) EXPR1 null , EXPR3  ,   EXPR1  ;        
    :expr1       ,expr2,expr3   long  。
             expr2     ,  expr3        (null  )。
             expr2     ,  expr3          。
                       。

(3)NULLIF 
  : NULLIF(EXPR1,EXPR2) 
   :(CASE WHEN EXPR1=EXPR2 THEN NULL ELSE EXPR1 END) 
  :NULLIF(EXPR1,EXPR2)   EXPR1 EXPR2  ,   NULL,    EXPR1;  EXPR1 EXPR2    NULL ,  NULL;
    :expr1      null,  nullif(null,expr2)       。
          expr1,expr2                   ,        。
       
(4)LNNVL
  : LNNVL(EXPR1)  EXPR1      
   :  a number(10);lnnvl(a>10)     nvl(a,0)<=10;     a<=10 or a is null;
  :  EXPR1    false   unknown,  lnnvl  true;  EXPR1    true,  false;
    :             AND, OR,  BETWEEN;

(5)COALESCE
  :COALESCE(EXPR_1,EXPR_2, ……,EXPR_n) 
   :(CASE WHEN EXPR_1 IS NULL THEN (CASE WHEN EXPR_2 IS NULL THEN (……,EXPR_n) ELSE EXPR_2 END) ELSE EXPR_1 END)
  :           NULL  ;
    :EXPR_1 EXPR_n         ;
  

SELECT NVL('G', 5),
       NVL(NULL, 5),
       NVL(NULL, SYSDATE),
       
       NVL2(NULL, 2, '3'),
       NVL2(0, 'a', 'b'),
       
       --NULLIF(NULL, 1),
       NULLIF(1, 1),
       NULLIF(1, 2),
       NULLIF('a', 'b'),
       
       (CASE
         WHEN LNNVL('a' = 'x1') THEN
          'a=x1:true'
         ELSE
          'a=x1:false'
       END),
       
       COALESCE(NULL, '', '3', 'a')

  FROM DUAL;


좋은 웹페이지 즐겨찾기