【SQL】使用一條INSERT語句完成多表插入
這是一條顛覆常規的插入方法,一條INSERT語句可以完成向多張表的插入任務。小小地展示一下這種插入方法。
1.建立表T並初始化測試資料,此表作為資料來源。
sec@ora10g> create table t (x number(10), y varchar2(10));
sec@ora10g> insert into t values (1,'a');
sec@ora10g> insert into t values (2,'b');
sec@ora10g> insert into t values (3,'c');
sec@ora10g> insert into t values (4,'d');
sec@ora10g> insert into t values (5,'e');
sec@ora10g> insert into t values (6,'f');
sec@ora10g> commit;
2.檢視錶T的資料
sec@ora10g> select * from t;
X Y
---------- ----------
1 a
2 b
3 c
4 d
5 e
6 f
6 rows selected.
3.建立表T1和T2,作為我們要插入的目標表。
sec@ora10g> create table t1 as select * from t where 0=1;
Table created.
sec@ora10g> create table t2 as select * from t where 0=1;
Table created.
4.第一種多表插入方法INSERT ALL
1)完成INSERT ALL插入
sec@ora10g> insert all into t1 into t2 select * from t;
12 rows created.
這裡之所以顯示插入了12條資料,實際上表示在T1表中插入了6條,T2表插入了6條,一共是12條資料。
2)驗證T1表中被插入的資料。
sec@ora10g> select * from t1;
X Y
---------- ----------
1 a
2 b
3 c
4 d
5 e
6 f
6 rows selected.
3)驗證T2表中被插入的資料。
sec@ora10g> select * from t2;
X Y
---------- ----------
1 a
2 b
3 c
4 d
5 e
6 f
6 rows selected.
OK,完成INSERT ALL命令的使命。
5.第二種多表插入方法INSERT FIRST
1)清空表T1和T2
sec@ora10g> delete from t1;
sec@ora10g> delete from t2;
sec@ora10g> commit;
2)完成INSERT FIRST插入
sec@ora10g> insert first when x>=5 then into t1 when x>=2 then into t2 select * from t;
5 rows created.
處理邏輯是這樣的,首先檢索T表查詢X列值大於等於5的資料(這裡是“5,e”和“6,f”)插入到T1表,然後將前一個查詢中出現的資料排除後再查詢T表,找到X列值大於等於2的資料再插入到T2表(這裡是“2,b”、“3,c”和“4,d”)。注意INSERT FIRST的真正目的是將同樣的資料只插入一次。
3)驗證T1表中被插入的資料。
sec@ora10g> select * from t1;
X Y
---------- ----------
5 e
6 f
4)驗證T2表中被插入的資料。
sec@ora10g> select * from t2;
X Y
---------- ----------
2 b
3 c
4 d
5)為真實的反映“資料只插入一次”的目的,我們把條件顛倒後再插入一次。
sec@ora10g> delete from t1;
sec@ora10g> delete from t2;
sec@ora10g> insert first when x>=2 then into t1 when x>=5 then into t2 select * from t;
5 rows created.
sec@ora10g> select * from t1;
X Y
---------- ----------
2 b
3 c
4 d
5 e
6 f
sec@ora10g> select * from t2;
no rows selected
OK,目的達到,可見滿足第二個條件的資料已經包含在第一個條件裡,所以不會有資料插入到第二張表。
同樣的插入條件,我們把“INSERT FIRST”換成“INSERT ALL”,對比一下結果。
sec@ora10g> delete from t1;
5 rows deleted.
sec@ora10g> delete from t2;
0 rows deleted.
sec@ora10g> insert all when x>=2 then into t1 when x>=5 then into t2 select * from t;
7 rows created.
sec@ora10g> select * from t1;
X Y
---------- ----------
2 b
3 c
4 d
5 e
6 f
sec@ora10g> select * from t2;
X Y
---------- ----------
5 e
6 f
是不是在豁然開朗的基礎上又有一種錦上添花的感覺。That's it.
6.Oralce官方文件參考連結
7.小結
這些小小小的高階SQL技巧在實際的應用中有很大用處。慢慢體會吧。
Good luck.
secooler
10.01.06
-- The End --
1.建立表T並初始化測試資料,此表作為資料來源。
sec@ora10g> create table t (x number(10), y varchar2(10));
sec@ora10g> insert into t values (1,'a');
sec@ora10g> insert into t values (2,'b');
sec@ora10g> insert into t values (3,'c');
sec@ora10g> insert into t values (4,'d');
sec@ora10g> insert into t values (5,'e');
sec@ora10g> insert into t values (6,'f');
sec@ora10g> commit;
2.檢視錶T的資料
sec@ora10g> select * from t;
X Y
---------- ----------
1 a
2 b
3 c
4 d
5 e
6 f
6 rows selected.
3.建立表T1和T2,作為我們要插入的目標表。
sec@ora10g> create table t1 as select * from t where 0=1;
Table created.
sec@ora10g> create table t2 as select * from t where 0=1;
Table created.
4.第一種多表插入方法INSERT ALL
1)完成INSERT ALL插入
sec@ora10g> insert all into t1 into t2 select * from t;
12 rows created.
這裡之所以顯示插入了12條資料,實際上表示在T1表中插入了6條,T2表插入了6條,一共是12條資料。
2)驗證T1表中被插入的資料。
sec@ora10g> select * from t1;
X Y
---------- ----------
1 a
2 b
3 c
4 d
5 e
6 f
6 rows selected.
3)驗證T2表中被插入的資料。
sec@ora10g> select * from t2;
X Y
---------- ----------
1 a
2 b
3 c
4 d
5 e
6 f
6 rows selected.
OK,完成INSERT ALL命令的使命。
5.第二種多表插入方法INSERT FIRST
1)清空表T1和T2
sec@ora10g> delete from t1;
sec@ora10g> delete from t2;
sec@ora10g> commit;
2)完成INSERT FIRST插入
sec@ora10g> insert first when x>=5 then into t1 when x>=2 then into t2 select * from t;
5 rows created.
處理邏輯是這樣的,首先檢索T表查詢X列值大於等於5的資料(這裡是“5,e”和“6,f”)插入到T1表,然後將前一個查詢中出現的資料排除後再查詢T表,找到X列值大於等於2的資料再插入到T2表(這裡是“2,b”、“3,c”和“4,d”)。注意INSERT FIRST的真正目的是將同樣的資料只插入一次。
3)驗證T1表中被插入的資料。
sec@ora10g> select * from t1;
X Y
---------- ----------
5 e
6 f
4)驗證T2表中被插入的資料。
sec@ora10g> select * from t2;
X Y
---------- ----------
2 b
3 c
4 d
5)為真實的反映“資料只插入一次”的目的,我們把條件顛倒後再插入一次。
sec@ora10g> delete from t1;
sec@ora10g> delete from t2;
sec@ora10g> insert first when x>=2 then into t1 when x>=5 then into t2 select * from t;
5 rows created.
sec@ora10g> select * from t1;
X Y
---------- ----------
2 b
3 c
4 d
5 e
6 f
sec@ora10g> select * from t2;
no rows selected
OK,目的達到,可見滿足第二個條件的資料已經包含在第一個條件裡,所以不會有資料插入到第二張表。
同樣的插入條件,我們把“INSERT FIRST”換成“INSERT ALL”,對比一下結果。
sec@ora10g> delete from t1;
5 rows deleted.
sec@ora10g> delete from t2;
0 rows deleted.
sec@ora10g> insert all when x>=2 then into t1 when x>=5 then into t2 select * from t;
7 rows created.
sec@ora10g> select * from t1;
X Y
---------- ----------
2 b
3 c
4 d
5 e
6 f
sec@ora10g> select * from t2;
X Y
---------- ----------
5 e
6 f
是不是在豁然開朗的基礎上又有一種錦上添花的感覺。That's it.
6.Oralce官方文件參考連結
7.小結
這些小小小的高階SQL技巧在實際的應用中有很大用處。慢慢體會吧。
Good luck.
secooler
10.01.06
-- The End --
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/519536/viewspace-624545/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Oracle 使用一條insert語句完成多表插入Oracle
- Oracle多表插入語句Oracle
- 【SQL】 Multi table insert 多表插入操作SQL
- Oracle的多表插入語句Oracle
- 一個insert插入語句很慢的優化優化
- 【SQL】9 SQL INSERT INTO 語句SQL
- 用一條mysql語句插入多條資料MySql
- SQL INSERT INTO 語句詳解:插入新記錄、多行插入和自增欄位SQL
- oracle insert all多表插入的示例Oracle
- Sql Server系列:Insert語句SQLServer
- oracle-資料庫- insert 插入語句Oracle資料庫
- 一條SQL語句的書寫SQL
- 一條很 巧妙的 SQL 語句SQL
- 一條sql語句的優化SQL優化
- 一條SQL語句的旅行之路SQL
- MySQL:使用INSERT 插入多條記錄MySql
- SQL語句批量插入資料SQL
- 一條insert語句導致的效能問題分析(一)
- 【INSERT】在INSERT插入語句中引入條件限制選項實現資料插入控制
- 一條sql語句的改進探索SQL
- 一條簡單SQL語句的構成及語句解析SQL
- OCI插入SQL語句的寫法SQL
- 一條insert語句導致的效能問題分析(二)
- 使用遊標迴圈進行SQL更新插入的SQL語句SQL
- Merge into: Oracle中用一條SQL語句直接進行Insert/Update的操作(R1)OracleSQL
- 利用SQL語句完成位操作 (轉)SQL
- 一條sql語句的執行過程SQL
- 一條SQL語句的優化過程SQL優化
- 每一千行UPDATE語句後插入一條COMMIT語句的編輯方法MIT
- 教你使用SQLite-insert語句SQLite
- SQL語句使用總結(一)SQL
- Mysql跨表更新 多表update sql語句總結MySql
- PL/SQL 條件控制語句SQL
- 【Mysql】兩條insert 語句產生的死鎖MySql
- 一條update SQL語句是如何執行的SQL
- 一條SQL更新語句是如何執行的SQL
- 一條SQL更新語句是如何執行的?SQL
- MyBatis 一次執行多條SQL語句MyBatisSQL