Oracle 基於其他表中的資料更新記錄時空值的處理方法
--建立測試資料
--將 employee_id 為 100 的 department_id 更新為空
SQL> create table employees as select * from hr.employees;
SQL> update employees set department_id=NULL where employee_id=100;
SQL> select * from employees;
EMPLOYEE_ID FIRST_NAM LAST_NAME EMAIL PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID
----------- --------- --------- --------- ------------------------- -------------------- ---------- ------ -------------- ---------- -------------
100 Steven King SKING 515.123.4567 2003-06-17 00:00:00 AD_PRES 24000
101 Neena Kochhar NKOCHHAR 515.123.4568 2005-09-21 00:00:00 AD_VP 17000 100 90
102 Lex De Haan LDEHAAN 515.123.4569 2001-01-13 00:00:00 AD_VP 17000 100 90
103 Alexander Hunold AHUNOLD 590.423.4567 2006-01-03 00:00:00 IT_PROG 9000 102 60
104 Bruce Ernst BERNST 590.423.4568 2007-05-21 00:00:00 IT_PROG 6000 103 60
105 David Austin DAUSTIN 590.423.4569 2005-06-25 00:00:00 IT_PROG 4800 103 60
106 Valli Pataballa VPATABAL 590.423.4560 2006-02-05 00:00:00 IT_PROG 4800 103 60
107 Diana Lorentz DLORENTZ 590.423.5567 2007-02-07 00:00:00 IT_PROG 4200 103 60
108 Nancy Greenberg NGREENBE 515.124.4569 2002-08-17 00:00:00 FI_MGR 12008 101 100
109 Daniel Faviet DFAVIET 515.124.4169 2002-08-16 00:00:00 FI_ACCOUNT 9000 108 100
110 John Chen JCHEN 515.124.4269 2005-09-28 00:00:00 FI_ACCOUNT 8200 108 100
--需求是:把所有員工的薪水修改成高於所屬部門平均薪水的 10%
--如果不對空值進行處理,直接更新的話,則 department_id 為空值的薪水會被更新成空值。因為在這種場景下,一個值為 NULL 的部門無法與另一個值為 NULL 的部門匹配,所以關聯子查詢會返回 NULL 。
SQL> update employees e
2 set salary =
3 (select avg(salary) * 1.10
4 from employees se
5 where se.department_id = e.department_id);
已更新107行。
SQL> select * from employees;
EMPLOYEE_ID FIRST_NAM LAST_NAME EMAIL PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID
----------- --------- --------- --------- ------------------------- -------------------- ---------- ------ -------------- ---------- -------------
100 Steven King SKING 515.123.4567 2003-06-17 00:00:00 AD_PRES
101 Neena Kochhar NKOCHHAR 515.123.4568 2005-09-21 00:00:00 AD_VP 18700 100 90
102 Lex De Haan LDEHAAN 515.123.4569 2001-01-13 00:00:00 AD_VP 18700 100 90
103 Alexander Hunold AHUNOLD 590.423.4567 2006-01-03 00:00:00 IT_PROG 6336 102 60
104 Bruce Ernst BERNST 590.423.4568 2007-05-21 00:00:00 IT_PROG 6336 103 60
105 David Austin DAUSTIN 590.423.4569 2005-06-25 00:00:00 IT_PROG 6336 103 60
106 Valli Pataballa VPATABAL 590.423.4560 2006-02-05 00:00:00 IT_PROG 6336 103 60
107 Diana Lorentz DLORENTZ 590.423.5567 2007-02-07 00:00:00 IT_PROG 6336 103 60
108 Nancy Greenberg NGREENBE 515.124.4569 2002-08-17 00:00:00 FI_MGR 9461 101 100
解決方法:
1、在 WHERE 字句中新增一個非空條件
SQL> update employees e
2 set salary =
3 (select avg(salary) * 1.10
4 from employees se
5 where se.department_id = e.department_id)
6 where department_id is not null;
已更新105行。
SQL> select * from employees;
EMPLOYEE_ID FIRST_NAM LAST_NAME EMAIL PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID
----------- --------- --------- --------- ------------------------- -------------------- ---------- ------ -------------- ---------- -------------
100 Steven King SKING 515.123.4567 2003-06-17 00:00:00 AD_PRES 24000
101 Neena Kochhar NKOCHHAR 515.123.4568 2005-09-21 00:00:00 AD_VP 18700 100 90
102 Lex De Haan LDEHAAN 515.123.4569 2001-01-13 00:00:00 AD_VP 18700 100 90
103 Alexander Hunold AHUNOLD 590.423.4567 2006-01-03 00:00:00 IT_PROG 6336 102 60
104 Bruce Ernst BERNST 590.423.4568 2007-05-21 00:00:00 IT_PROG 6336 103 60
105 David Austin DAUSTIN 590.423.4569 2005-06-25 00:00:00 IT_PROG 6336 103 60
106 Valli Pataballa VPATABAL 590.423.4560 2006-02-05 00:00:00 IT_PROG 6336 103 60
107 Diana Lorentz DLORENTZ 590.423.5567 2007-02-07 00:00:00 IT_PROG 6336 103 60
108 Nancy Greenberg NGREENBE 515.124.4569 2002-08-17 00:00:00 FI_MGR 9461 101 100
2、使用 NVL 函式來處理 NULL 值
SQL> update employees e
2 set salary =
3 nvl((select avg(salary) * 1.10
4 from employees se
5 where se.department_id = e.department_id),salary);
已更新107行。
SQL> select * from employees;
EMPLOYEE_ID FIRST_NAM LAST_NAME EMAIL PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID
----------- --------- --------- --------- ------------------------- -------------------- ---------- ------ -------------- ---------- -------------
100 Steven King SKING 515.123.4567 2003-06-17 00:00:00 AD_PRES 24000
101 Neena Kochhar NKOCHHAR 515.123.4568 2005-09-21 00:00:00 AD_VP 18700 100 90
102 Lex De Haan LDEHAAN 515.123.4569 2001-01-13 00:00:00 AD_VP 18700 100 90
103 Alexander Hunold AHUNOLD 590.423.4567 2006-01-03 00:00:00 IT_PROG 6336 102 60
104 Bruce Ernst BERNST 590.423.4568 2007-05-21 00:00:00 IT_PROG 6336 103 60
105 David Austin DAUSTIN 590.423.4569 2005-06-25 00:00:00 IT_PROG 6336 103 60
106 Valli Pataballa VPATABAL 590.423.4560 2006-02-05 00:00:00 IT_PROG 6336 103 60
107 Diana Lorentz DLORENTZ 590.423.5567 2007-02-07 00:00:00 IT_PROG 6336 103 60
108 Nancy Greenberg NGREENBE 515.124.4569 2002-08-17 00:00:00 FI_MGR 9461 101 100
--將 employee_id 為 100 的 department_id 更新為空
SQL> create table employees as select * from hr.employees;
SQL> update employees set department_id=NULL where employee_id=100;
SQL> select * from employees;
EMPLOYEE_ID FIRST_NAM LAST_NAME EMAIL PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID
----------- --------- --------- --------- ------------------------- -------------------- ---------- ------ -------------- ---------- -------------
100 Steven King SKING 515.123.4567 2003-06-17 00:00:00 AD_PRES 24000
101 Neena Kochhar NKOCHHAR 515.123.4568 2005-09-21 00:00:00 AD_VP 17000 100 90
102 Lex De Haan LDEHAAN 515.123.4569 2001-01-13 00:00:00 AD_VP 17000 100 90
103 Alexander Hunold AHUNOLD 590.423.4567 2006-01-03 00:00:00 IT_PROG 9000 102 60
104 Bruce Ernst BERNST 590.423.4568 2007-05-21 00:00:00 IT_PROG 6000 103 60
105 David Austin DAUSTIN 590.423.4569 2005-06-25 00:00:00 IT_PROG 4800 103 60
106 Valli Pataballa VPATABAL 590.423.4560 2006-02-05 00:00:00 IT_PROG 4800 103 60
107 Diana Lorentz DLORENTZ 590.423.5567 2007-02-07 00:00:00 IT_PROG 4200 103 60
108 Nancy Greenberg NGREENBE 515.124.4569 2002-08-17 00:00:00 FI_MGR 12008 101 100
109 Daniel Faviet DFAVIET 515.124.4169 2002-08-16 00:00:00 FI_ACCOUNT 9000 108 100
110 John Chen JCHEN 515.124.4269 2005-09-28 00:00:00 FI_ACCOUNT 8200 108 100
--需求是:把所有員工的薪水修改成高於所屬部門平均薪水的 10%
--如果不對空值進行處理,直接更新的話,則 department_id 為空值的薪水會被更新成空值。因為在這種場景下,一個值為 NULL 的部門無法與另一個值為 NULL 的部門匹配,所以關聯子查詢會返回 NULL 。
SQL> update employees e
2 set salary =
3 (select avg(salary) * 1.10
4 from employees se
5 where se.department_id = e.department_id);
已更新107行。
SQL> select * from employees;
EMPLOYEE_ID FIRST_NAM LAST_NAME EMAIL PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID
----------- --------- --------- --------- ------------------------- -------------------- ---------- ------ -------------- ---------- -------------
100 Steven King SKING 515.123.4567 2003-06-17 00:00:00 AD_PRES
101 Neena Kochhar NKOCHHAR 515.123.4568 2005-09-21 00:00:00 AD_VP 18700 100 90
102 Lex De Haan LDEHAAN 515.123.4569 2001-01-13 00:00:00 AD_VP 18700 100 90
103 Alexander Hunold AHUNOLD 590.423.4567 2006-01-03 00:00:00 IT_PROG 6336 102 60
104 Bruce Ernst BERNST 590.423.4568 2007-05-21 00:00:00 IT_PROG 6336 103 60
105 David Austin DAUSTIN 590.423.4569 2005-06-25 00:00:00 IT_PROG 6336 103 60
106 Valli Pataballa VPATABAL 590.423.4560 2006-02-05 00:00:00 IT_PROG 6336 103 60
107 Diana Lorentz DLORENTZ 590.423.5567 2007-02-07 00:00:00 IT_PROG 6336 103 60
108 Nancy Greenberg NGREENBE 515.124.4569 2002-08-17 00:00:00 FI_MGR 9461 101 100
解決方法:
1、在 WHERE 字句中新增一個非空條件
SQL> update employees e
2 set salary =
3 (select avg(salary) * 1.10
4 from employees se
5 where se.department_id = e.department_id)
6 where department_id is not null;
已更新105行。
SQL> select * from employees;
EMPLOYEE_ID FIRST_NAM LAST_NAME EMAIL PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID
----------- --------- --------- --------- ------------------------- -------------------- ---------- ------ -------------- ---------- -------------
100 Steven King SKING 515.123.4567 2003-06-17 00:00:00 AD_PRES 24000
101 Neena Kochhar NKOCHHAR 515.123.4568 2005-09-21 00:00:00 AD_VP 18700 100 90
102 Lex De Haan LDEHAAN 515.123.4569 2001-01-13 00:00:00 AD_VP 18700 100 90
103 Alexander Hunold AHUNOLD 590.423.4567 2006-01-03 00:00:00 IT_PROG 6336 102 60
104 Bruce Ernst BERNST 590.423.4568 2007-05-21 00:00:00 IT_PROG 6336 103 60
105 David Austin DAUSTIN 590.423.4569 2005-06-25 00:00:00 IT_PROG 6336 103 60
106 Valli Pataballa VPATABAL 590.423.4560 2006-02-05 00:00:00 IT_PROG 6336 103 60
107 Diana Lorentz DLORENTZ 590.423.5567 2007-02-07 00:00:00 IT_PROG 6336 103 60
108 Nancy Greenberg NGREENBE 515.124.4569 2002-08-17 00:00:00 FI_MGR 9461 101 100
2、使用 NVL 函式來處理 NULL 值
SQL> update employees e
2 set salary =
3 nvl((select avg(salary) * 1.10
4 from employees se
5 where se.department_id = e.department_id),salary);
已更新107行。
SQL> select * from employees;
EMPLOYEE_ID FIRST_NAM LAST_NAME EMAIL PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID
----------- --------- --------- --------- ------------------------- -------------------- ---------- ------ -------------- ---------- -------------
100 Steven King SKING 515.123.4567 2003-06-17 00:00:00 AD_PRES 24000
101 Neena Kochhar NKOCHHAR 515.123.4568 2005-09-21 00:00:00 AD_VP 18700 100 90
102 Lex De Haan LDEHAAN 515.123.4569 2001-01-13 00:00:00 AD_VP 18700 100 90
103 Alexander Hunold AHUNOLD 590.423.4567 2006-01-03 00:00:00 IT_PROG 6336 102 60
104 Bruce Ernst BERNST 590.423.4568 2007-05-21 00:00:00 IT_PROG 6336 103 60
105 David Austin DAUSTIN 590.423.4569 2005-06-25 00:00:00 IT_PROG 6336 103 60
106 Valli Pataballa VPATABAL 590.423.4560 2006-02-05 00:00:00 IT_PROG 6336 103 60
107 Diana Lorentz DLORENTZ 590.423.5567 2007-02-07 00:00:00 IT_PROG 6336 103 60
108 Nancy Greenberg NGREENBE 515.124.4569 2002-08-17 00:00:00 FI_MGR 9461 101 100
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26506993/viewspace-2015795/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- oracle中undo表空間丟失處理方法Oracle
- vue中當資料為空時的處理Vue
- SAP CRM中介軟體下載時資料庫表CRMATAB為空的處理方法資料庫
- 關於丟失表空間資料檔案的處理方式
- 影像資料不足時的處理方法
- Oracle臨時表空間檢視、新增臨時表空間資料檔案、修改預設臨時表空間 方法!Oracle
- Oracle排程作業引起的空間驟增問題處理記錄Oracle
- Oracle資料庫出現ORA-19566 LOB壞塊的處理記錄Oracle資料庫
- 【C#】-對於Null值的處理方法C#Null
- sysaux 表空間爆滿處理方法UX
- mybatis+oracle 批次插入多條資料的處理方法MyBatisOracle
- oracle建立臨時表空間和資料表空間以及刪除Oracle
- Python資料分析基礎: 資料缺失值處理Python
- 【Python資料分析基礎】: 資料缺失值處理Python
- Oracle資料庫中的逐行處理問題NEOracle資料庫
- oracle sysaux表空間滿了處理辦法OracleUX
- mysql 批次更新與批次更新多條記錄的不同值實現方法MySql
- 由於目錄變更引起的資料服務無法啟動的問題處理記錄
- oracle系統表空間過大問題處理Oracle
- Oracle更新Opatch故障處理Oracle
- 時間序列資料的處理
- 基於python的大資料分析-資料處理(程式碼實戰)Python大資料
- 如何處理Oracle資料庫中的壞塊問題(轉)Oracle資料庫
- 如何在SQLServer中處理每天四億三千萬記錄的SQLServer
- 【筆記】基於Python的數字影象處理筆記Python
- [python] 基於Tablib庫處理表格資料Python
- 資料處理踩過的坑(不斷更新):
- 基於flink和drools的實時日誌處理
- oracle 觸發器,當一個表更新或插入時將資料同步至另個庫中的某個表中Oracle觸發器
- Spark在處理資料的時候,會將資料都載入到記憶體再做處理嗎?Spark記憶體
- Oracle資料使用者密碼過期處理方法Oracle密碼
- 反映在基於資料感知的智慧資料預處理和智慧排程
- ORACLE中seq$表更新頻繁的分析Oracle
- config表與其他資料表的關聯
- 機器學習中資料缺失的處理及建模方法機器學習
- oracle 臨時表空間的增刪改查Oracle
- 小程式處理大量資料列表的方法
- oracle遊標批次處理資料Oracle
- ORACLE資料庫基於時間點的不完全恢復Oracle資料庫