一、insert插入数据
1、将整张表的数据插入到一张表中
语法:insert into 表名(子查询)
使用子查询将整张表插入到制定的表中,有两种方式,如下:
(1)、insert into cip_test (select *from cip_tmp)。
(2)、insert /*+append*/into cip_test (select *from cip_tmp);
注意:如果要插入大量的数据,则采用第二种方式,效率高于第一种,insert数据会直接加到表的最后面,而不会在表的空闲块中插入数据,使用append会增加数据插入的速度。网上说"append 属于direct insert,归档模式下append+table nologging会大量减少日志,非归档模式append会大量减少日志,append方式插入只会产生很少的undo
",有点不明白,待研究。我按照第二种方法插入50000条数据,结果toad死掉了,不知道为什么。
2、将整张表的输入插入到多个表中
(1)、使用all操作符执行插入操作
使用insert语句可以将某张表的数据同时插入到多张表中,语法如下:
insert all insert_into_clause[value_clause] 子查询;
如上所示:insert_into_clause指insert子句,value_clause指定值子句insert all into cip_test into cip_temp select * from cip_tmp where id<10;
插入的数据中cip_test表中的数据为1—10,cip_temp表中的数据为1—10,cip_temps表中的数据为1—10。
insert all
when id between 1 and 10 then into cip_test
when id between 11 and 20 then into cip_temp
when id between 21 and 30 then into cip_temps
select * from cip_tmp where id<30;
插入的数据中cip_test表中的数据为1—10,cip_temp表中的数据为11—20,cip_temps表中的数据为21—30。
注意:SQL语法1是将查询的信息全部插入的指定的表中。SQL2语句2是将查询的结果按照条件插入到指定的表中。
(2)、使用first操作符执行插入操作
当使用first操作符插入多表数据时,当先前条件已经满足,并且已经插入到表中,则该数据在后续插入中将不会在被用到。代码例如:
insert first
when id <=10 then into cip_test
when id <=20 then into cip_temp
when id <=30 then into cip_temps
select * from cip_tmp where id<30;
插入的数据中cip_test表中的数据为1—10,cip_temp表中的数据为11—20,cip_temps表中的数据为21—30。
二、update更新数据
当使用update语句更新数据时,不仅可以使用表达式、数值直接更新数据,可可以使用子查询更新数据,某些情况下使用子查询更新效率更好,例如:
update cip_temp set (name,age,address)=(select name,age,address from cip_test where id=1) where id=20;[/code
[b]三、delete删除数据[/b]
当使用delete语句删除数据时,可以在where子句中指定值,并根据条件删除数据,另外也可以再where子句中使用子查询做为条件删除数据,例如:
delete from cip_temp where name=(select name from cip_test where id=1)