Oracle基础.docx
- 文档编号:15430926
- 上传时间:2023-07-04
- 格式:DOCX
- 页数:29
- 大小:24.47KB
Oracle基础.docx
《Oracle基础.docx》由会员分享,可在线阅读,更多相关《Oracle基础.docx(29页珍藏版)》请在冰点文库上搜索。
Oracle基础
目录
Oracle基础3
第一天教学内容3
数据库三范式:
3
Oracle简单介绍:
3
Oracle的安装:
3
数据库自带用户:
3
Sql*plus相关设置及操作:
3
简单查询:
4
标量函数和算数运算:
4
Or与union、unionall:
5
And与intersect:
5
Minus:
5
第二天教学内容6
笛卡尔积:
6
内连接6
自连接6
多表关联查询:
6
外连接6
指定操作关联列:
using7
自然连接:
7
子查询:
7
子查询中in/any/all的应用:
7
第三天教学内容7
列函数7
分组:
8
having字句的使用:
8
例子(与with条件的比较):
8
书写顺序:
8
执行顺序:
9
关于分组查询的一个例子:
9
decode函数:
9
第四天教学内容13
备份表:
13
创建表:
13
插入数据:
13
多行插入数据:
13
有条件的插入数据:
14
修改记录:
14
清除数据:
14
增加列:
14
删除列:
14
删除行:
14
修改列:
14
重命名:
15
约束:
15
索引:
15
分区表:
15
临时表:
15
约束有效16
视图:
16
数据字典17
分析函数:
17
伪列:
18
第五天教学内容18
用户:
18
表空间18
同义词:
19
程序结构:
19
Oracle基础教学
第一天教学内容
数据库三范式:
列的原子性
非主属性对主属性的完全依赖(不可以部分依赖)
不存在非主属性废任意主属性的传递函数依赖(不满足A->B,B->C,则A->C的关系)
Oracle简单介绍:
Oracle8
Oracle8i互联网
Oracle9i
Oracle10g网格
Oracle的安装:
用户解锁:
图形化界面命令行
数据库自带用户:
Scott:
tiger
System:
manager普通管理员
Sys:
change_on_install超级管理员
Sql*plus相关设置及操作:
Ed文件名
@路径
Setlinesize
Setpagesize
简单查询:
表结构:
desc表名
用户的表:
selecttable_namefromuser_tables
查看用户:
showuser
查询所有列:
查询指定列:
加别名:
消除重复行:
distinct
限定查询:
where
(isnull/isnotnull/and/or/not/betweenand/in/orderby(desc/asc))
模糊查询:
like_表示一个字符,%表示若干字符
标量函数和算数运算:
大小写转换:
upper/lower
首字母大写:
initcap
连接:
concat(||)
截取字符串:
substr从1开始与从0开始结果是一样的,-1指从倒数第一个字符开始
求长度:
length
串替换:
replace
四舍五入:
round(指定保留小数位数/对整数四舍五入)
截断小数位:
trunc(也可以截断整数)
取余:
mod
查看当前日期:
sysdate
增加月份:
selectadd_months(sysdate,4)fromdual;
月份数:
selectmonths_between(sysdate,hiredate)fromemp;
最后一天:
Last_day
转换函数:
to_char,fm,to_date
selectto_char(sysdate,'fmyyyy-mm-dd-dy-q')fromdual;
selectto_date('2009-09-23','yyyy-mm-dd')fromdual;
金额显示的技巧:
selectto_char(sal,'$99,999')fromemp;
空值处理:
selectnvl(comm,0)fromemp;
ASCII码的转换:
selectASCII('a')fromdual;
selectchr(97)fromdual;
selecttrim(leading'x'from'xxxxscottxxxx')fromdual;//leading是指前头即左侧
selecttrim(trailing'x'from'xxxxscottxxxx')fromdual;//trailing指后头即右侧
selecttrim(both'x'from'xxxxscottxxxx')fromdual;//both指两侧
selectltrim('xxxxscottxxxx','x')fromdual;//从左侧删除
selectrtrim('xxxxscottxxxx','x')fromdual;//从右侧删除
selecttrim('xxxxscottxxxx')fromdual;//删除两侧空格
selectlpad('dasfs',13,3)fromdual;//从左侧补全13位,补上元素3.
selectrpad('dasfs',13,3)fromdual;从右侧补全13位,补元素3
selectlength(ltrim(‘a’))fromdual;
selectlength(rtrim(‘a’))fromdual;
selectvsize('哈哈')fromdual;//返回字符长度
LENGTH(string1)返回以字符为单位的长度.
LENGTHB(string1)返回以字节为单位的长度.
LENGTHC(string1)返回以Unicode完全字符为单位的长度.
LENGTH2(string1)返回以UCS2代码点为单位的长度.
LENGTH4(string1)返回以UCS4代码点为单位的长度.
Or与union、unionall:
selectenamefromempwheresal>2000orcommisnotnull;
selectenamefromempwheresal>2000
union//把两者中的重复给去掉了。
selectenamefromempwherecommisnotnull;
UNIONALL操作符返回两个查询的结果集的并集以及两个结果集的重复部分(不去重)
selectenamefromempwheresal>2000orcommisnotnull;
selectenamefromempwheresal>2000
unionall
selectenamefromempwherecommisnotnull;
And与intersect:
selectenamefromempwheresal>2000andcommisnull;
selectenamefromempwheresal>2000
intersect
selectenamefromempwherecommisnull;
Minus:
selectenamefromempwheresal>2000
minus
selectenamefromempwherecommisnotnull;
第二天教学内容
笛卡尔积:
select*fromemp,dept;
select*fromempcrossjoindept;
内连接
(内连接把两个表连接成一个表(称为第三个表),在这个表中仅包含那些满足连接条件的记录行):
Select*fromemp,deptwhereemp.deptno=dept.deptno;
自连接
(自连接就是把某一张表中的行同该表中另外一些行连接起来):
selecte.empno,e.ename,a.ename,a.mgrfromempe,empawheree.mgr=a.empno;
多表关联查询:
selecte.ename,e.sal,b.grade,a.ename,a.sal,d.grade
fromempe,empa,salgradeb,salgraded
wheree.salbetweenb.losalandb.hisal
anda.salbetweend.losalandd.hisal
ande.mgr=a.empno;
外连接
(如果想只限定一个表的条件而不限定另外一个表的条件,就需要使用外连接):
select*fromemp,deptwhereemp.deptno=dept.deptno;
select*fromemp,deptwhereemp.deptno(+)=dept.deptno;
select*fromemprightjoindeptonemp.deptno=dept.deptno;
select*fromdeptleftjoinempondept.deptno=emp.deptno;
指定操作关联列:
using
select*fromempjoindeptusing(deptno);
自然连接:
select*fromempnaturaljoindept;
子查询:
selectsalfromempwhereempno=7654;
selectenamefromempwheresal>1250;
selectenamefromempwheresal>(selectsalfromempwhereempno=7654);
子查询中in/any/all的应用:
selectenamefromempwheresal>all(selectsalfromempwheredeptno=20);
selectenamefromempwheresal>any(selectsalfromempwheredeptno=20);
selectenamefromempwheresalin(selectsalfromempwheredeptno=20);
第三天教学内容
列函数
sum(),min(),max(),count()
selectcount(empno),//查询记录数。
max(sal),
min(sal),
avg(nvl(comm,0)),//如果comm为空,则取0.
sum(nvl(comm,0))
fromemp;
分组:
selectdeptno,count(empno)fromempgroupbydeptno;
规则:
不在列函数中的列,必须全出现在groupby之后
having字句的使用:
//过滤
selectavg(sal)fromempgroupbyjobhavingavg(sal)>800;
例子(与with条件的比较):
WITH
dept_costsAS(
SELECTd.dname,SUM(e.sal)ASdept_sum
FROMempe,deptd
WHEREe.deptno=d.deptno
GROUPBYd.dname),
avg_costAS(
SELECTSUM(dept_sum)/COUNT(*)ASdept_avg
FROMdept_costs)
SELECT*
FROMdept_costs
WHEREdept_sum>
(SELECTdept_avg
FROMavg_cost)
ORDERBYdname;
SELECTd.dname,SUM(e.sal)ASdept_total
FROMempe,deptd
WHEREe.deptno=d.deptno
GROUPBYd.dname
havingSUM(e.sal)>(selectsum(sal)/3fromemp);
书写顺序:
selectavg(sal)
fromemp
wheredeptnoin(10,20)
groupbyjob
havingavg(sal)>800
orderbyavg(sal)desc;
执行顺序:
From
Where
Groupby
Having
Select
Orderby
关于分组查询的一个例子:
createtablestu(namevarchar2(10)notnull,
numnumber(10),
knamevarchar2(20),
scorenumber(10)check(score>=0andscore<=100));
selects.*,s.rowidfromstus;
selectname,min(score)fromstugroupbynamehavingmin(score)>=75;
decode函数:
selectcount(empno)fromempgroupbyhiredate;
selectsum(count(empno)),
sum(decode(to_char(hiredate,'yyyy'),'1980',count(empno),0))"1980",
sum(decode(to_char(hiredate,'yyyy'),'1981',count(empno),0))"1981",
sum(decode(to_char(hiredate,'yyyy'),'1982',count(empno),0))"1982",
sum(decode(to_char(hiredate,'yyyy'),'1987',count(empno),0))"1987"
fromempgroupbyto_char(hiredate,'yyyy');
selectsum(count(empno)),
max(decode(to_char(hiredate,'yyyy'),'1980',count(empno),0))"1980",
max(decode(to_char(hiredate,'yyyy'),'1981',count(empno),0))"1981",
max(decode(to_char(hiredate,'yyyy'),'1982',count(empno),0))"1982",
max(decode(to_char(hiredate,'yyyy'),'1987',count(empno),0))"1987"
fromempgroupbyto_char(hiredate,'yyyy');
selectsum(count(empno)),
min(decode(to_char(hiredate,'yyyy'),'1980',count(empno)))"1980",
min(decode(to_char(hiredate,'yyyy'),'1981',count(empno)))"1981",
min(decode(to_char(hiredate,'yyyy'),'1982',count(empno)))"1982",
min(decode(to_char(hiredate,'yyyy'),'1987',count(empno)))"1987"
fromempgroupbyto_char(hiredate,'yyyy');
例子1
创建表:
createtablestu(idnumber,knamevarchar(6),scorenumber);
插入值:
//无论是在dos下还是SQL窗口中,修改后只有提交才能完成,例在dos下用commit提交后在SQL窗口中才能查询到修改后的数据。
insertintoSTU(ID,KNAME,SCORE)
values(1,'yu',100);
insertintoSTU(ID,KNAME,SCORE)
values(2,'yu',98);
insertintoSTU(ID,KNAME,SCORE)
values(2,'shu',54);
insertintoSTU(ID,KNAME,SCORE)
values(3,'yu',89);
insertintoSTU(ID,KNAME,SCORE)
values(3,'shu',100);
insertintoSTU(ID,KNAME,SCORE)
values(3,'ying',21);
commit;
查看数据:
select*fromstu;
1.行列互换:
selectdecode(id,1,'一年级',2,'二年级',3,'三年级')"年级",
decode(kname,'yu',score,0)"语文",
decode(kname,'shu',score,0)"数学",
decode(kname,'ying',score,0)"英语"fromstu;
selectdecode(id,1,'一年级',2,'二年级',3,'三年级')"年级",
sum(decode(kname,'yu',score,0))"语文",
sum(decode(kname,'shu',score,0))"数学",
sum(decode(kname,'ying',score,0))"英语"fromstugroupbyid;
selectdecode(id,1,'一年级',2,'二年级',3,'三年级')"年级",
max(decode(kname,'yu',score,0))"语文",
max(decode(kname,'shu',score,0))"数学",
max(decode(kname,'ying',score,0))"英语"fromstugroupbyid;
selectdecode(id,1,'一年级',2,'二年级',3,'三年级')"年级",
min(decode(kname,'yu',score))"语文",
min(decode(kname,'shu',score))"数学",
min(decode(kname,'ying',score))"英语"fromstugroupbyid;
selectdecode(id,1,'一年级',2,'二年级',3,'三年级')"年级",
decode(id,1,100,2,98,3,89)"语文",
decode(id,1,0,2,54,3,100)"数学",
decode(id,1,0,2,0,3,21)"英语"
fromstu
groupbyid;
例子二
表、视图结构转化
现有一个商品销售表sale,表结构为:
month char(6) --月份
sell number(10,2) --月销售金额
createtableSALE
(
MONVARCHAR2(8),
SELLNUMBER
);
insertintoSALE(MON,SELL)
values('20001',1000);
insertintoSALE(MON,SELL)
values('20002',1100);
insertintoSALE(MON,SELL)
values('20003',1200);
insertintoSALE(MON,SELL)
values('20004',1300);
insertintoSALE(MON,SELL)
values('20005',14);
insertintoSALE(MON,SELL)
values('20006',1500);
insertintoSALE(MON,SELL)
values('20007',1879);
insertintoSALE(MON,SELL)
values('20011',1526);
insertintoSALE(MON,SELL)
values('20012',15222);
commit;
selectdecode(substr(mon,0,4),2000,'2000年',2001,'2001年')"年份",
decode(substr(mon,5),1,sell,0)"1",
decode(substr(mon,5),2,sell,0)"2",
decode(substr(mon,5),3,sell,0)"3",
decode(substr(mon,5),4,sell,0)"4",
decode(substr(mon,5),5,sell,0)"5",
decode(substr(mon,5),6,sell,0)"5",
decode(substr(mon,5),7,sell,0)"7"
fromsale;
selectdecode(substr(mon,0,4),2000,'2000年',2001,'2001年')"年份",
sum(decode(substr(mon,5),1,sell,0))"1",
sum(decode(substr(mon,5),2,sell,0))"2",
sum(decode(substr(mon,5),3,sell,0))"3",
sum(decode(substr(mon,5),4,sell,0))"4",
sum(decode(substr(mon,5),5,sell,0))"5",
sum(decode(substr(mon,5),6,sell,0))"5",
sum(decode(substr(mon,5),
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Oracle 基础