【SQL】使用一条INSERT语句完成多表插入
来源:互联网 发布:linux蓝牙终端 编辑:程序博客网 时间:2024/06/06 10:50
这是一条颠覆常规的插入方法,一条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官方文档参考链接
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_9014.htm#SQLRF01604
7.小结
这些小小小的高级SQL技巧在实际的应用中有很大用处。慢慢体会吧。
Good luck.
secooler
10.01.06
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官方文档参考链接
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_9014.htm#SQLRF01604
7.小结
这些小小小的高级SQL技巧在实际的应用中有很大用处。慢慢体会吧。
Good luck.
secooler
10.01.06
-- The End --
0 0
- 【SQL】使用一条INSERT语句完成多表插入
- 【SQL】使用一条INSERT语句完成多表插入
- 使用一条INSERT语句完成多表插入
- 使用一条INSERT语句完成多表插入
- Oracle 使用一条insert语句完成多表插入
- SQL Server2005 不支持一条insert语句插入多条记录
- MySQL使用一条INSERT语句插入多条记录
- 使用“一条”SQL语句完成批量插入和批量删除操作
- 关于SQL Server2005支不支持一条insert语句插入多条记录的问题
- sql server 2005生成insert语句,同时完成多表
- 用一条insert语句来插入多行数据
- Oracle:用一条 INSERT 语句批量插入多条记录
- 一条insert语句批量插入多条记录
- 一条insert语句批量插入多条记录
- 用一条insert语句来插入多行数据
- 一条insert语句批量插入多条记录
- mysql中一条insert语句批量插入多条记录
- 一条SQL语句 插入多条数据
- java 编程环境
- SQLite中使用全文搜索FTS
- MATLAB之imfilter和fspecial函数
- C++explicit的使用
- 使用PHPStorm 配置自定义的Apache与PHP环境
- 【SQL】使用一条INSERT语句完成多表插入
- stl string常用函数
- 微信小程序开发:实时圆形进度条实现
- QML如何结合Qt使用GridView
- 用Monitor简单3步监控ActiveMQ
- build-in webserver in PhpStorm
- 正则表达式总结
- 热更新Tinker研究(六):TinkerPatchPlugin
- Windows bat脚本之for循环之 一