要學的東西很多,一直在努力中............! 希望佛祖赐予我智慧吧!阿门!!!

PLSQL常用方法汇总

上一篇 / 下一篇  2008-06-05 16:46:37 / 个人分类:ORACLE 開發

在SQLPLUS下,实现中-英字符集转换
`"l8@dWv0alter session set nls_language='AMERICAN';ITPUB个人空间,p,f8hM#l M BB
alter session set nls_language='SIMPLIFIED CHINESE';ITPUB个人空间G(O5A3|CN lZ
主要知识点:
U_ w6b^@0一、有关表的操作ITPUB个人空间E6P"k4c!a~[ g
1)建表
)S BHQ#u0create table test as select * from dept; --从已知表复制数据和结构ITPUB个人空间5@7jTz5~!EK@
create table test as select * from dept where 1=2; --从已知表复制结构但不包括数据ITPUB个人空间H5Vo-~T9~~ ga}/h
2)插入数据:ITPUB个人空间-k G-fN E
insert into test select * from dept;
K\p6? ^6Ui4Yw0二、运算符ITPUB个人空间E6K8?Zh0Q/~
算术运算符:+ - * / 可以在select 语句中使用
l/Y7w+Q.][3J8k7T"Fb0连接运算符:|| select deptno|| dname from dept;
`ZUS sM|;J0比较运算符:> >= = != < <= like between is null in
6Lv&vi|!wXF!h0逻辑运算符:not and or
V,q4q` }F&_0集合运算符: intersect ,union, union all, minus
I4l.{ lp1tO ?0要求:对应集合的列数和数据类型相同
^y"m!|wCM*Gg0查询中不能包含long 列
4z9Yi l F0列的标签是第一个集合的标签
3ra/R4cA&P0使用order by时,必须使用位置序号,不能使用列名ITPUB个人空间+_-ju Iv1gh(p
例:集合运算符的使用:ITPUB个人空间$I3mE ax{{
intersect ,union, union all, minus
2sA'ZT,a0|7HZu)W0select * from emp intersect select * from emp where deptno=10 ;
8[F!jV+k,k0select * from emp minus select * from emp where deptno=10;ITPUB个人空间Xd i7hR2A
select * from emp where deptno=10 union select * from emp where deptno in (10,20); --不包括重复行ITPUB个人空间s9v2_'|9Ws8X,^U{
select * from emp where deptno=10 union all select * from emp where deptno in (10,20); --包括重复行
7V&CfpjG+z0ITPUB个人空间.b9[6F qJ7` mG$gu%{
三,常用 ORACLE 函数
o-{zg3S c0sysdate为系统日期 dual为虚表
FW(KI\}6Mm^0一)日期函数[重点掌握前四个日期函数]
f Z&cqp#k0p01,add_months[返回日期加(减)指定月份后(前)的日期]ITPUB个人空间ADW"[G j-ZR7X'v;| j
select sysdate S1,add_months(sysdate,10) S2,ITPUB个人空间(}B9n sQ+[L}gV;B
add_months(sysdate,5) S3 from dual;ITPUB个人空间U)| m2F/\ N
2,last_day [返回该月最后一天的日期]ITPUB个人空间o j*^V*@C!p5i
select last_day(sysdate) from dual;
\{k I.G|03,months_between[返回日期之间的月份数]ITPUB个人空间if_'@ ~!oxz"b Y w
select sysdate S1, months_between('1-4月-04',sysdate) S2,
et9Dv:f0K$y0months_between('1-4月-04','1-2月-04') S3 from dual
V#Z ]tj bMa,M04,next_day(d,day): 返回下个星期的日期,day为1-7或星期日-星期六,1表示星期日ITPUB个人空间S-ChZt \} Q
select sysdate S1,next_day(sysdate,1) S2,ITPUB个人空间 A6T:DUp:E#\
next_day(sysdate,'星期日') S3 FROM DUAL
j#H?z*x05,round[舍入到最接近的日期](day:舍入到最接近的星期日)
I s/d{Gq^0select sysdate S1,ITPUB个人空间*X+N2DK%A&n1|Z
round(sysdate) S2 ,
Mh;T&j4Dw2a0round(sysdate,'year') YEAR,ITPUB个人空间OBz;WM4|4S
round(sysdate,'month') MONTH ,
Mi0~Pi0round(sysdate,'day') DAY from dualITPUB个人空间(~~]K$C
6,trunc[截断到最接近的日期]
eQ} QK.| eJ0select sysdate S1,ITPUB个人空间LG*l%Aj?A
trunc(sysdate) S2,ITPUB个人空间2~DB3QF7h m+O-T
trunc(sysdate,'year') YEAR,
vY^%BLxb1F0trunc(sysdate,'month') MONTH ,ITPUB个人空间D8eQ3HW3]V7r U B8fp6~
trunc(sysdate,'day') DAY from dualITPUB个人空间 M:y$auQ"pq%aE
7,返回日期列表中最晚日期
~ l#A$~8W vp#vbS0select greatest('01-1月-04','04-1月-04','10-2月-04') from dualITPUB个人空间7y%M#MR yF
ITPUB个人空间A"p$c#T,cl|pn$G
二)字符函数(可用于字面字符或数据库列)
7pM y#K{-a01,字符串截取
v e%~D t;b0select substr('abcdef',1,3) from dualITPUB个人空间1bIem(Spj M
2,查找子串位置
@2`3A,VU*? y)o ?4M0select instr('abcfdgfdhd','fd') from dualITPUB个人空间e,tt$m m0vsuU
3,字符串连接ITPUB个人空间 c(v'E_\-dZ IE
select 'HELLO'||'hello world' from dual;
xG!J$u8?)Ki04, 1)去掉字符串中的空格ITPUB个人空间.wP5{y"R6z-g(J!KgD
select ltrim(' abc') s1,
#r"{+Qe(J#a)}J8C7?0rtrim('zhang ') s2,
z#e @,NDND+N"~{HQ&g:L0trim(' zhang ') s3 from dualITPUB个人空间!~#\&S"`uY1G
2)去掉前导和后缀ITPUB个人空间J!SB^_uM u
select trim(leading 9 from 9998767999) s1,
#y'P h'anES0trim(trailing 9 from 9998767999) s2,ITPUB个人空间x;O,A_NO2i
trim(9 from 9998767999) s3 from dual;ITPUB个人空间L_ w0k+X {HC G;m&^
5,返回字符串首字母的Ascii值
,u7Hx1VT0select ascii('a') from dualITPUB个人空间*q0} Nq5tA z:\ rH
6,返回ascii值对应的字母
6j2K1Aqv]0select chr(97) from dualITPUB个人空间)V[%d/uN*v6m
7,计算字符串长度ITPUB个人空间A#HE9mvT
select length('abcdef') from dual
ct@a)K&YV08,initcap(首字母变大写) ,lower(变小写),upper(变大写)ITPUB个人空间 J&n1I/z8Jy
select lower('ABC') s1,
Y,Q&{ K4kJ(s-gZQV0upper('def') s2,ITPUB个人空间0qR#G7?+Nj%S#\
initcap('efg') s3 from dual;ITPUB个人空间$x\)C8?D{W
9,ReplaceITPUB个人空间L0\1K*W6q pj
select replace('abc','b','xy') from dual;ITPUB个人空间:LR2q] xb1A
10,translateITPUB个人空间0rml7G7Is
select translate('abc','b','xx') from dual; -- x是1位
1ps"]8T"h!f~011,lpad [左添充] rpad [右填充](用于控制输出格式)ITPUB个人空间9TAl,`7bt'L-Y
select lpad('func',15,'=') s1, rpad('func',15,'-') s2 from dual;
ao [al+B"FDH0select lpad(dname,14,'=') from dept;ITPUB个人空间,f(s,ceK+WT-H1Q
12, decode[实现if ..then 逻辑]
'm.AQ{KLh0select deptno,decode(deptno,10,'1',20,'2',30,'3','其他') from dept;
(Y yYOHC0pZ3c0三)数字函数
qyE$[`LhI:N01,取整函数(ceil 向上取整,floor 向下取整)ITPUB个人空间$Q0V)bn0]*D-lb
select ceil(66.6) N1,floor(66.6) N2 from dual;
r:w+Z9r L)Y9Tu4A-?k02, 取幂(power) 和 求平方根(sqrt)
(F"Lp.j#x _0select power(3,2) N1,sqrt(9) N2 from dual;ITPUB个人空间!ohn8j0A
3,求余
1o!g)o9er0l;T0select mod(9,5) from dual;ITPUB个人空间(|:Ny$|Ne
4,返回固定小数位数 (round:四舍五入,trunc:直接截断)ITPUB个人空间aA:hU._.c)N^
select round(66.667,2) N1,trunc(66.667,2) N2 from dual;
(B\EpM!PQUn05,返回值的符号(正数返回为1,负数为-1)ITPUB个人空间D#A*v[ EQ\n
select sign(-32),sign(293) from dual;
*k5{ D K&D5V:}%k-[a0

四)转换函数ITPUB个人空间"h6EL;r/TuxNZM
1,to_char()[将日期和数字类型转换成字符类型]
F9\z$zRsN01) select to_char(sysdate) s1,
&NEU%}~ r.Q0to_char(sysdate,'yyyy-mm-dd') s2,ITPUB个人空间"\3n ~I.j
to_char(sysdate,'yyyy') s3,
*B M)h }8UX!?#O;^0to_char(sysdate,'yyyy-mm-dd hh12:mi:ss') s4,ITPUB个人空间&P t,d!moKR6Tb
to_char(sysdate, 'hh24:mi:ss') s5,ITPUB个人空间-tN,X u? |
to_char(sysdate,'DAY') s6 from dual;ITPUB个人空间"DXoFH
2) select sal,to_char(sal,'$99999') n1,to_char(sal,'$99,999') n2 from emp
ne4IK;AZsB02, to_date()[将字符类型转换为日期类型]ITPUB个人空间oJ#O#l]6D%Y`'B
insert into emp(empno,hiredate) values(8000,to_date('2004-10-10','yyyy-mm-dd'));ITPUB个人空间_ F&g/h\T&yL
3, to_number() 转换为数字类型
Ey he;h&A6W*g0select to_number(to_char(sysdate,'hh12')) from dual; //以数字显示的小时数ITPUB个人空间*ZE?K3x
五)其他函数ITPUB个人空间 FT nhq@E!yP
user:ITPUB个人空间J&N#X'UX:b(o
返回登录的用户名称ITPUB个人空间(e7{aC9l&o-p
select user from dual;ITPUB个人空间cT+Z$w"fo4s
vsize:
-K4M'K&v/`/f4m\0返回表达式所需的字节数ITPUB个人空间4]0R,wd~L(L }
select vsize('HELLO') from dual;
y[^W,y7d0nvl(ex1,ex2):  ITPUB个人空间U*[ K2} S9r7e7w
ex1值为空则返回ex2,否则返回该值本身ex1(常用)
1hwVDRU0例:如果雇员没有佣金,将显示0,否则显示佣金ITPUB个人空间5S5k~ aQ7hE
select comm,nvl(comm,0) from emp;ITPUB个人空间&q!}4`e,a0oI9E;^v
nullif(ex1,ex2):ITPUB个人空间-D^M"[:v2wtf
值相等返空,否则返回第一个值
.G&r*Na)ED2D0例:如果工资和佣金相等,则显示空,否则显示工资
I+DD |I#Li XX \;J0select nullif(sal,comm),sal,comm from emp;
i5Unk0zv(S f0coalesce:  ITPUB个人空间"`.tZ5u1\
返回列表中第一个非空表达式
0nF)V6q6^C!N0select comm,sal,coalesce(comm,sal,sal*10) from emp;
"aI6aF2ujE)nO0   nvl2(ex1,ex2,ex3) :ITPUB个人空间 |Uu`6E:v%SnvK@ e
如果ex1为空,显示ex2,否则显示ex3ITPUB个人空间,Q'uPW%Q
如:查看有佣金的雇员姓名以及他们的佣金ITPUB个人空间.kg9d7[n
     select nvl2(comm,ename,') as HaveCommName,comm from emp;ITPUB个人空间P gQ J%W*iZ9pq2I&P
六)分组函数
D,v@:hg`[)Iu|+D0max min avg count sumITPUB个人空间n*jUX#Nt4^~
1,整个结果集是一个组
k2@:hW9c+}q7f01) 求部门30 的最高工资,最低工资,平均工资,总人数,有工作的人数,工种数量及工资总和
}Vp1^,Cm,{5n/K~.q0select max(ename),max(sal),
o$ptPO8csz7X,o0min(ename),min(sal),
.V kY4W#p"zwR0f eF0avg(sal),
"v2dCd LO$?0count(*) ,count(job),count(distinct(job)) ,ITPUB个人空间GZ"Y\$s$y
sum(sal) from emp where deptno=30;ITPUB个人空间pl5mv(zM.UhP4^
2, 带group by 和 having 的分组ITPUB个人空间@rPxD_2\E
1)按部门分组求最高工资,最低工资,总人数,有工作的人数,工种数量及工资总和
7u6\0{SR6A0select deptno, max(ename),max(sal),
6uFQ8I9`rY0min(ename),min(sal),
euP'|)^!`8wiK*B@0avg(sal),ITPUB个人空间!e)UR XV
count(*) ,count(job),count(distinct(job)) ,ITPUB个人空间A)i XA q;JF6zk
sum(sal) from emp group by deptno;ITPUB个人空间tIrFl
2)部门30的最高工资,最低工资,总人数,有工作的人数,工种数量及工资总和ITPUB个人空间XA Sc1Vu cr%`
select deptno, max(ename),max(sal),ITPUB个人空间 I#z5m/C^3P4T
min(ename),min(sal),ITPUB个人空间b/EH1|*E)V3E xH
avg(sal),
"i1H9x(\Q0count(*) ,count(job),count(distinct(job)) ,
I/v:M;y#i.Bt E1qj0sum(sal) from emp where deptno=30 group by deptno ;ITPUB个人空间$n)F"jtmMu
3, stddev 返回一组值的标准偏差
F N8GG/q4x en EoU4`0select deptno,stddev(sal) from emp group by deptno;ITPUB个人空间.\C"lw/p zW
variance 返回一组值的方差差
PD n@5FV1g)P7q0select deptno,variance(sal) from emp group by deptno;
7hV"\+[b7|)i04, 带有rollup和cube操作符的Group By
*L4X \'{ f mq}[0rollup 按分组的第一个列进行统计和最后的小计
(T3l&T!z9HIT dH&f0cube 按分组的所有列的进行统计和最后的小计ITPUB个人空间!r(o"~%S"Xj
select deptno,job ,sum(sal) from emp group by deptno,job;
n,T}P^Z%Q@0select deptno,job ,sum(sal) from emp group by rollup(deptno,job);ITPUB个人空间V7\*W,Y0T7g@6o)@
cube 产生组内所有列的统计和最后的小计ITPUB个人空间S.br&EwbU,~E!I d
select deptno,job ,sum(sal) from emp group by cube(deptno,job);
1v9`x-xWbWc1k ^w0
B,m6e*n8YL6d0七、临时表ITPUB个人空间C6b2| k,q6j|6WW i
只在会话期间或在事务处理期间存在的表.ITPUB个人空间1\&i `e/p Xy
临时表在插入数据时,动态分配空间ITPUB个人空间|n \1WK!nw
create global temporary table temp_dept
5AgC Y-A6b g0(dno number,
ui Zn:qy0dname varchar2(10))
|u'i:SZxz0on commit delete rows;ITPUB个人空间 Ly#m\;fgA'w
insert into temp_dept values(10,'ABC');ITPUB个人空间9zOz ]EY(y+f
commit;
&tS)E7_^6}%_I0select * from temp_dept; --无数据显示,数据自动清除
${g.K [R l!C0on commit preserve rows:在会话期间表一直可以存在(保留数据)ITPUB个人空间/q"rU6O;r _5p-E i%J
on commit delete rows:事务结束清除数据(在事务结束时自动删除表的数据)


TAG:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2008-10-13  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 7585
  • 日志数: 429
  • 图片数: 1
  • 影音数: 1
  • 建立时间: 2007-12-13
  • 更新时间: 2008-10-06

RSS订阅

Open Toolbar