使用一条INSERT语句完成多表插入

跟版网(www.genban.org)提供跟版网精品网站模板,跟版网模板,网站模板,等网页设计素材资源,提供相关网页设计资源的教程和免费下载。跟版网,专业织梦网页设计模板资源站。。

这是一条颠覆常规的插入方法,一条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.

#p#副标题#e#

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命令的使命。

#p#副标题#e#

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

#p#副标题#e#

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 --

 

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

数据库查询哪个对像里面包含什么字段语句写法: select * from sysobjects o, syscomments s where o.id = s.id and text like %text% and o.xtype = P text 换成需要查的字段 数据库查询哪个对像里面包含表: select o.name from sys.all_sql_modules s,sys
一、 创建用户: 命令:CREATE USER username@host IDENTIFIED BY password; 说明:username - 你将创建的用户名, host - 指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost, 如果想让该用户可以从任意远程主机登陆,可以使用通配符%. password - 该
在mysql中可以用group by对查询出的数据分组 select id,service,name FROM service GROUP BY name,service 如果要查看每组数据的总数,可以 select count(*) FROM service GROUP BY name,service 当要查询group by后的总数,可以这样 select count(*) from(s
mysql count group by统计条数方法 mysql 分组之后如何统计记录条数? gourp by 之后的 count,把group by查询结果当成一个表再count一次 select count(*) as count from(SELECT count(*) FROM 表名 WHERE 条件 GROUP BY id ) a; 实战例子: select count(*)
1.首先停止MySQL服务:service mysqld stop 2.加参数启动mysql:/usr/bin/mysqld_safe --skip-grant-tables 然后就可以无任何限制的访问mysql了 3.root用户登陆系统:mysql -u root -p mysql 4.切换数据库:use mysql 5.显示所有的表:show tables; 这里就可
摘要: SQL的WHERE子句中包含多个AND和OR 示例: SQL解析器在处理操作时会优先处理and操作: 假如有表product字段如下:id、product_id、product_price、product_name,现在要查找产品号为100或者101,并且价格大于200的商品,程序员可能会这样写: select * fr