基本操作
1) desc,describe 作用是顯示數(shù)據(jù)表的結(jié)構(gòu) 使用形式: desc 數(shù)據(jù)表名
2) distinct 消除重復(fù)的數(shù)據(jù) 使用形式:select distinct 字段名 from 數(shù)據(jù)表
3) order by 字段1 asc,字段2 desc
4) 嵌套查詢(xún) select emp.empno,emp.ename,emp.job,emp.sal
from scott.emp
where sal>=(select sal from scott.emp where ename = 'WARD');
5) in 嵌套查詢(xún) select emp.empno,emp.ename,emp.job,emp.sal
from scott.emp
where sal in (select sal from scott.emp where ename = 'WARD');
6) any 嵌套查詢(xún) select emp.empno,emp.ename,emp.job,emp.sal
from scott.emp
where sal > any (select sal from scott.emp where job = 'MANAGER');
等價(jià)于 (1) select sal from scott.emp where job = 'MANAGER'
(2) select emp.empno,emp.ename,emp.job,emp.sal
from scott.emp
where sal > (1)中查出的數(shù)據(jù)a or sal > (1)中查出的數(shù)據(jù)b or sal > (1)中查出的數(shù)據(jù)c
eg:
select sal from scott.emp where job = 'MANAGER'結(jié)果;12,10,13
等價(jià)于 sal=12,10,13 或者 SAL>(12 OR 10 OR 13)
7) 交操作就是集合中交集的概念农尖。屬于集合A且屬于集合B的元素總和就是交集凿试。 在【命令編輯區(qū)】執(zhí)行下列語(yǔ)句。
eg:
(select djbh from ck_rwd_hz) intersect(select djbh from ck_rwd_mx) 單據(jù)編號(hào)相同的
select * from ck_rwd_mx a,
((select djbh from ck_rwd_hz) intersect(select djbh from ck_rwd_mx)) b
where a.djbh =b.djbh
二 函數(shù)
1)ceil 取大于等于數(shù)值N的最小整數(shù) ceil(N); select mgr,mgr/100,ceil(mgr/100) from scott.emp;
2)floor 取小于等于數(shù)值N的最大整數(shù) floor(N); select mgr,mgr/100,floor(mgr/100) from scott.emp;
3)mod m整除n后的余數(shù) mod(m,n)
4)power m的n次方 mod(m,n)
5)round m的四舍五入,保留n位 mod(m,n)
select round(8.655,2)from dual; 8.66
select round(8.653,2)from dual; 8.65
6)sign n>0,取1;n=0,取0;n<0,取-1;
7)avg 求平均值 avg(字段名)
8)count 統(tǒng)計(jì)總數(shù) count(字段名) select (*) from scott.emp; select count(distinct job) from scott.emp;
9)min 計(jì)算數(shù)值型字段最小值 select min(sal) 最少薪水 from scott.emp;
10)max 計(jì)算數(shù)值型字段最大值 select max(sal) 最高薪水 from scott.emp;
11)sum 計(jì)算數(shù)值型字段總和 select sum(sal) 薪水總和 from scott.emp;
三 錄入數(shù)據(jù)
1)單行數(shù)據(jù)錄入
insert into 數(shù)據(jù)表(字段1除盏,字段2,...)valuse(字段名1的值挫以,字段名2的值者蠕,...)
數(shù)值型字段可以直接寫(xiě)值;字符型字段加單引號(hào)掐松;日期型字段加單引號(hào)踱侣,同時(shí)要注意年月日的排列順序
2)多行數(shù)據(jù)錄入
insert into 數(shù)據(jù)表(字段名1粪小,字段名2,...)
(select(字段名1或運(yùn)算抡句,字段名2或運(yùn)算探膊,...)from數(shù)據(jù)表 where 條件)
3)表間的數(shù)據(jù)復(fù)制
create table scott.test
as
(
select distinct empno,ename,hiredate from scott.emp where empno>=7000
);
create table spkpk_liu as select * from spkfk; 創(chuàng)建表又復(fù)制數(shù)據(jù),但是創(chuàng)建的表信息不全
全表備份的時(shí)候可以這樣些待榔。
一般在建了表之后根據(jù)需要再看是否要把索引和主鍵這些再建上去逞壁。
而且" create table spkpk_liu as select * from spkfk
"
這樣建表后,表的很多參數(shù)值都是默認(rèn)的最小的值究抓,比如原表的初始值10M猾担,新表的可能只有256K。
正式的環(huán)境里面用到的表刺下,一般不推薦這樣建表。
用這個(gè)方法僅僅有點(diǎn)偷懶稽荧,這樣做的話橘茉,一個(gè)語(yǔ)句可以實(shí)現(xiàn)建表和插入數(shù)據(jù)的目的。
比如需要修改表A的數(shù)據(jù)姨丈,修改前可能考慮把A表的數(shù)據(jù)備份一個(gè)畅卓。
這個(gè)時(shí)候就可以用create table ... as...
這樣方便將來(lái)回復(fù)A表的數(shù)據(jù)
自己調(diào)試程序的時(shí)候都可以這樣處理 但是不能像這樣創(chuàng)建過(guò)程、包蟋恬、函數(shù)
四 刪除數(shù)據(jù)
delete 刪除數(shù)據(jù)翁潘; truncate刪除整個(gè)表數(shù)據(jù)但保留結(jié)構(gòu)
1)刪除記錄
delete from scott.test where empno >= 7500 and empno <=8000;
2)刪除整個(gè)數(shù)據(jù)
truncate table scott.test;
truncate,delete,drop的異同點(diǎn)
注意:這里說(shuō)的delete是指不帶where子句的delete語(yǔ)句
相同點(diǎn):truncate和不帶where子句的delete, 以及drop都會(huì)刪除表內(nèi)的數(shù)據(jù)
不同點(diǎn):
1. truncate和 delete只刪除數(shù)據(jù)不刪除表的結(jié)構(gòu)(定義)
drop語(yǔ)句將刪除表的結(jié)構(gòu)被依賴(lài)的約束(constrain),觸發(fā)器(trigger),索引(index); 依賴(lài)于該表的存儲(chǔ)過(guò)程/函數(shù)將保留,但是變?yōu)閕nvalid狀態(tài).
2.delete語(yǔ)句是dml,這個(gè)操作會(huì)放到rollback segement中,事務(wù)提交之后才生效;如果有相應(yīng)的trigger,執(zhí)行的時(shí)候?qū)⒈挥|發(fā).
truncate,drop是ddl, 操作立即生效,原數(shù)據(jù)不放到rollback segment中,不能回滾. 操作不觸發(fā)trigger. 顯然drop語(yǔ)句將表所占用的空間全部釋放
3.速度,一般來(lái)說(shuō): drop> truncate > delete
使用上,想刪除部分?jǐn)?shù)據(jù)行用delete,注意帶上where子句. 回滾段要足夠大,可以通過(guò)ROBACK回滾歼争,相當(dāng)有恢復(fù)的余地
想刪除表,當(dāng)然用drop
想保留表而將所有數(shù)據(jù)刪除. 如果和事務(wù)無(wú)關(guān),用truncate即可. truncate table xx 刪除整個(gè)表的數(shù)據(jù)拜马,沒(méi)有恢復(fù)的余地,好處可以整理表內(nèi)碎片沐绒,釋放空間
所以最好一般先備份數(shù)據(jù)
如果是整理表內(nèi)部的碎片,可以用truncate跟上reuse stroage,再重新導(dǎo)入/插入數(shù)據(jù)
五 更新數(shù)據(jù)
update 數(shù)據(jù)表
set 字段名1=新的賦值俩莽,字段名2=新的賦值,....
where 條件
update scott.emp
set empno=8888,ename='TOM',hiredate='03-9月 -2002'
where empno = 7566;
update scott.emp
set sal=
(select sal+300 from scott.emp where empno = 8099)
where empno=8099;
decode(條件,值1,翻譯值1,值2,翻譯值2,...值n,翻譯值n,缺省值)