一、多行函數又稱組合函數(Group Functions)、聚合函數 
1、 Types of Group Functions 
avg、count、max、min、stddev、sum、variance 
avg 求平均數 
select avg(nvl(列1,0)) from 表1 
count求行數 
在where條件中不允許使用聚合函數,但可以使用having avg(列1)>1000 
having所起的作用和where一樣 
二、子查詢Subqueries 
查詢前10行數據 
oracle: select * from 表名 where rownum<=10; 
sql: select top 10 * from 表名 
單行子查詢 
select * from 表1 where 工資列1>(select avg(工資列1) from 表1) 
多行子查詢 
select * from 表1 where 工資列1 in(select min(工資列1) from 表1 group by 部門列) 
三、自定義變量 
set verify on/off 
show all 
help show/set 
column lie justify left 
四、數據操作語句 
1、insert插入語句 
向表2里插入數據 
oracle:insert into (select 列1,列2 from 表2)values('XXX','XXX'); 
oracle/sql:insert into(列1,列2)values('XXX','XXX'); 
從另一個表里復制數據 
oracle/sql:insert into 表(列1,列2)select 列1,列2 from 表2 
2、update語句 
都為: update table set column1='...'[ ,column2='...'] where ... 
嵌入子查詢的修改 
update table set column1=(select column2 form table where columnid=1) where column1='...' 
delete刪除語句 
delete [from] table [where condition] 
merge 合并語句 
oracle: 
merge into 表1 a using 表2 b on (a.id=b.id) 
when matched then 
update set 
a.name=b.name, 
a.other=b.other 
when not matched then 
insert values(b.id,b.name,b.other); 
sql:合并insert,update 
方法1: 
declare @ROWCOUNT int 
set @ROWCOUNT=(select count(*) from tb_name where name1='5') 
if @ROWCOUNT!=0 
update tb_name set name2='55555555' where name1='5' 
else 
insert into tb_name(name1,name2) values('5','插入') 
方法2: 
update tb_name set name2='55555555' where name1='6' 
if @@ROWCOUNT=0 
insert into tb_name(name1,name2) values('6','插入') 
五,事務: 隱式、顯式的事務 
commit提交事務 
rollback 回滾事務 
locking鎖 
對并發性系統自動加鎖,事務提交后、或回滾后自動解鎖。 
新聞熱點
疑難解答