oracle中的null

來源:互聯網
上載者:User

標籤:

在我們不知道具體有什麼資料的時候,也即未知,可以用NULL,我們稱它為空白,ORACLE中,含有空值的表列長度為零。
 
ORACLE允許任何一種資料類型的欄位為空白,除了以下兩種情況:
1、主鍵欄位(primary key),
2、定義時已經加了NOT NULL限制條件的欄位
 
說明:
1、等價於沒有任何值、是未知數,NULL與0、Null 字元串、空格都不同。
  SQL> SELECT 1 FROM dual WHERE ‘‘=‘‘;
  未選擇任何行
  SQL> select 1 from dual where ‘‘=null;
  未選擇任何行
  SQL> SELECT 1 FROM dual WHERE NULL = NULL;
  未選擇任何行
  SQL> select 1 from dual where null = 0;        
  未選擇任何行 
  SQL> select 1 from dual where ‘‘ is null;
           1
  ----------
           1  
  SQL> SELECT 1 FROM dual WHERE NULL IS NULL;
           1
  ----------
           1
2、對空值做加、減、乘、除等運算操作,結果仍為空白。如果where條件中出現這種比較,就是未知,相當於false,進而不返回資料
   SQL> select 1 from dual where (select 1+null from dual) is null
   SQL> select 1 from dual where (select 1-null from dual) is null
   SQL> select 1 from dual where (select 1*null from dual) is null
   SQL> select 1 from dual where (select 1/null from dual) is null
  
   註:not in的情況
   SQL> select 1 from dual where ‘b‘ not in (‘a‘, null)  
   SQL> select 1 from dual where ‘b‘!=‘a‘ and ‘b‘!=null
   -- 以上兩句是對等的,因為‘b‘!=null始終返回的是null,所以真值運算式也始終不成立,進而不返回任何的資料。
   -- 正確的做法應該是排除null的情況,比如在括弧中排除null值的出現
3、NULL的處理使用NVL函數。
   SQL> select 1 from dual where nvl(null,0)=nvl(null,0);
4、比較時使用關鍵字用“is null”和“is not null”。
   SQL> select 1 from dual where ‘‘ is null;
   SQL> select 1 from dual where ‘‘ is not null;
5、空值不能被索引,所以查詢時有些合格資料可能查不出來,count(*)中,用nvl(列名,0)處理後再查。
   ????
6、排序
  1)不加“關照”的情況下,我們可以把那些NULL值假想為所有內容中值是最大的,因此,升序排序後NULL值在最後,倒序排序後NULL值在最前!
  2)特殊“關照”的情況下,當指定“NULLS FIRST”時,無論是升序排序還是倒序排序,NULL值都會排列在最前面;當指定“NULLS LAST”時,無論是升序排序還是倒序排序,NULL值都會排列在最後面。

7、大多數的彙總函式會忽略null,例如
  1)avg,1000、null、null、2000,使用avg取平均值,實際上是(1000+2000)/2,所以特殊情況下,最好是取和然後在除以結果集數量
  2)count,1000、null、null、2000,得到的結果是2,最好是使用count(1)或count(*)

oracle中的null

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.