oracle學(xué)習(xí)筆記(二)
一、多行函數(shù)又稱(chēng)組合函數(shù)(Group Functions)、聚合函數(shù)
1、 Types of Group Functions
avg、count、max、min、stddev、sum、variance
avg 求平均數(shù)
select avg(nvl(列1,0)) from 表1
count求行數(shù)
在where條件中不允許使用聚合函數(shù),但可以使用having avg(列1)>1000
having所起的作用和where一樣
二、子查詢(xún)Subqueries
查詢(xún)前10行數(shù)據(jù)
oracle: select * from 表名 where rownum<=10;
sql: select top 10 * from 表名
單行子查詢(xún)
select * from 表1 where 工資列1>(select avg(工資列1) from 表1)
多行子查詢(xún)
select * from 表1 where 工資列1 in(select min(工資列1) from 表1 group by 部門(mén)列)
三、自定義變量
set verify on/off
show all
help show/set
column lie justify left
四、數(shù)據(jù)操作語(yǔ)句
1、insert插入語(yǔ)句
向表2里插入數(shù)據(jù)
oracle:insert into (select 列1,列2 from 表2)values('XXX','XXX');
oracle/sql:insert into(列1,列2)values('XXX','XXX');
從另一個(gè)表里復(fù)制數(shù)據(jù)
oracle/sql:insert into 表(列1,列2)select 列1,列2 from 表2
2、update語(yǔ)句
都為: update table set column1='...'[ ,column2='...'] where ...
嵌入子查詢(xún)的修改
update table set column1=(select column2 form table where columnid=1) where column1='...'
delete刪除語(yǔ)句
delete [from] table [where condition]
merge 合并語(yǔ)句
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','插入')
五,事務(wù): 隱式、顯式的事務(wù)
commit提交事務(wù)
rollback 回滾事務(wù)
locking鎖
對(duì)并發(fā)性系統(tǒng)自動(dòng)加鎖,事務(wù)提交后、或回滾后自動(dòng)解鎖。
相關(guān)文章
SQL Server 實(shí)現(xiàn)數(shù)字輔助表實(shí)例代碼
這篇文章主要介紹了SQL Server 實(shí)現(xiàn)數(shù)字輔助表的相關(guān)資料,并附實(shí)例代碼,需要的朋友可以參考下2016-10-10
有關(guān)sqlserver帳號(hào)被禁用的處理方法
這篇文章主要介紹了有關(guān)sqlserver帳號(hào)被禁用處理方法,需要的朋友可以參考下2017-12-12
WIN10運(yùn)行SQL2000安裝程序時(shí)沒(méi)有反應(yīng)的問(wèn)題解決辦法
Win10系統(tǒng)上安裝2000數(shù)據(jù)庫(kù)或者msde的數(shù)據(jù)庫(kù)經(jīng)常在打開(kāi)第一步安裝的時(shí)候就報(bào)錯(cuò)或者沒(méi)有反應(yīng),文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06
SQL?Server實(shí)現(xiàn)group_concat函數(shù)的詳細(xì)舉例
這篇文章主要給大家介紹了關(guān)于SQL?Server實(shí)現(xiàn)group_concat函數(shù)的詳細(xì)舉例,GROUP_CONCAT函數(shù)可以拼接某個(gè)字段值成字符串,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11
sqlserver數(shù)據(jù)庫(kù)移動(dòng)數(shù)據(jù)庫(kù)路徑的腳本示例
前段時(shí)間做過(guò)這么一件事情,把原本放在c盤(pán)的所有數(shù)據(jù)庫(kù)(除了sql server系統(tǒng)文件外)文件Move到D盤(pán),主要是為了方便后續(xù)管理以及減少磁盤(pán)I/O阻塞(C,D是2個(gè)獨(dú)立磁盤(pán))。腳本需輸入2個(gè)參數(shù):目標(biāo)數(shù)據(jù)庫(kù)名字和目標(biāo)目錄2013-12-12

