当前位置:文档之家› oracle知识点总结

oracle知识点总结

==================================================================================
==================================================================================

﹡首先配置环境:开始-设置-控制面板-管理工具-服务-OracleServiceO9I-启动

1、sqlplus的使用
(1)开始-运行-cmd-sqlplus-输入用户名和口令
(2)开始-运行-sqlplus-输入用户名和口令
(3)退出sqlplus环境,用exit或者quit

2、介绍几个常用的oracle用户
超级管理员 sys 默认口令: change_on_install oracle
管理员 system 默认口令 manager oracle
scott tiger
sh sh

==================================================================================

3、关于oracle sql*plus环境使用的几点常用命令。
(1) show user //查看当前用户名称
(2) 用户解锁的方法(本命令一般是由管理员来完成的)
alter user sh identified by sh account
unlock; //解锁sh用户的方法
(3) 在sqlplus环境中切换用户的方法
conn 用户名/口令
(4) 查看当前用户所有表的命令。
select * from tab;
select * from cat;
(5) 查看表的结构的命令
desc 表名

https://www.doczj.com/doc/755909405.html,
course201009@https://www.doczj.com/doc/755909405.html,
oracle2010

==================================================================================

2.输出结果的排序:order by排序列名desc|asc
eg:
①查找工资超过1500的员工的信息,并按员工名降序输出。
select * from emp where sal>1500 order by ename desc;
注意:排序永远是查询的最后一个步骤。

②查找工资超过2000的员工的工资、姓名和参加工作时间,并按工资多少进行升序排列,
若工资相同,则按员工名降序输出。
select sal,ename,hiredate from emp where sal>2000
order by sal asc,ename desc;

用列名、表达式、列号获得系统的当前时间:
select sysdate from dual;

==================================================================================

练习:
找出工作超过3500天的员工姓名、工号、其工作天数和工资,并按工资降序输出,若工资相同,
则按工作天数升序,若工作天数也一样,则按员工工号降序输出
select ename,empno,sysdate-hiredade
tianshu,sal from where (sysdate-hiredata)>3500
order by sal desc,tianshu asc,empno desc;

1.向一个表添加多条记录
insert into table_name() values();
insert into table_name() select字句;

eg:
create table test(id char(4),name varchar2(20),score number(5,2));
insert into test select'1111','zhao',58.5 from dual
union select'1112','zhang',98 from dual;//dual是伪表

2.聚组函数:这类函数要返回一个汇总值。
count
max
min
avg
sum
(1)函数的参数可以是列名,也可以是表达式,其中count函数的参数可以是*。
(2)count(*),这时不忽

略空值。
(3)avg sum的参数只能是数值型
(4)空值问题 //任何数和空值运算时是空值
nvl(参数1,参数2)

eg:①显示员工的最高月收入 select max(sal+comm)from emp;
②显示每个员工的月收入 select sal+nvl(comm,0)from emp;
③计算每年发给员工的钱是多少
④查看所有员工的平均奖金
⑤计算公司每个部门的平均工资,要求显示部门号和平均工资值
select deptno,avg(sal)from emp group by deptno;

==================================================================================

☆group by子句
当select子句中包含了除了聚组函数表达式外的其他的单值列时,该列必须作为分组列来处理。
⑥计算每个部门中每个工种的人数
select deptno,job,count(*)from emp group by deptno,job;
⑦显示平均工资超过2000的部门号和其平均工资
select deptno,avg(sal) from emp
where avg(sal)>2000
group by deptno having avg(sal)>2000;


☆having子句(是对分组以后的结果进行筛选)

(5)聚组函数可以出现在select子句
eg:查询20,30号部门的平均工资
select deptno,avg(sal) from emp where deptno=20 or deptno=30;
显示20,30号部门平均工资超过1500的部门号和平均工资
select deptno,avg(sal)from emp where avg(sal)>1500

==================================================================================

9周
连接查询
1、等值连接(内连接)
完全列名 表别名
eg:①查询所有员工的姓名和所在部门的名称(emp,dept)
select ename,dname from emp,dept where emp.deptno=dept.deptno;

②查询20和30号部门员工的姓名和所在部门的名称(emp,dept)
select emp.ename,dept.dname from emp,dept
where emp.deptno=dept.deptno
and emp.deptno in(20,30);

等值连接的另一种表示方法:a inner join b on 连接条件
select emp.ename,dept.dname from emp
inner join dept on emp .deptno=dept.deptno
where emp.deptno in(20,30);

③查询以M开头的所有员工的姓名、工龄和工作所在地点
select emp.ename,
emp.to_char(sysdate,'yyyy')-to_char(emp.hiredate,'yyyy')工龄,
dept.loc from emp,dept
where emp.deptno=dept.deptno
and ename like'%M';
简化(别名)select e.ename,
e.to_char(sysdate,'yyyy')-to_char(e.hiredate,'yyyy')工龄,
d.loc from emp e,dept d
where e.deptno=d.deptno
and e.ename like'M%';

==================================================================================

2、自连接
eg:显示每个员工的经理名
select e.ename,m.ename from emp e,emp m where e.mgr=m.empno;

3、不等连接
eg:计算每个员工的工资等级salgrade
select e.ename,e.sal,s.grade from emp e,salgrade s
where e.sal between s.losal and s.hisal;
输出所有部门的员工的名字和所在部门名称
select d.deptno,e.ename,d.dname from dept d,emp e
where d.deptno=e.deptno;

当需要输出既满足条件的记录,又要输出不满足连接条件

的纪录时,需要用到外连接。
在外连接中,我们给出两个概念:基表,匹配表。基表的所有数据全部输出,匹配表除了输出
与基表相匹配的记录外,对于不匹配的记录,匹配表中相应的字段的值为空值。

a left outer join b on(左外连接)连接条件
right 右
full (+)(永远跟在匹配表的后边)

select e.deptno,e.ename,d.dname from dept d,emp e
where d.deptno=e.deptno(+);//左外连接
select d.deptno,e.ename,d.dname
from dept d left outer join emp e on d.deptno=e.deptno(+);

select d.deptno,e.ename,d.dname from dept d,emp e
where e.deptno(+)=d.deptno;//右外连接

1、列出各种工作职位的最低工资,且显示最低工资大于1500的记录
select losal from salgrade

2、列出从事同一种工作但属于不同部门的雇员的员工信息
select a.ename,a.job,a.deptno from emp a,emp b where a.job=b.job and a.deptno!=b.deptno;

3、列出至少有一个员工的所有部门

4、列出各个部门的MANAGER(经理)的最低薪金

==================================================================================

10周

子查询(一个select语句包含在其他的sql语句中。子查询可以出现在where,having,from
子句中,用括号把子查询括起来)//先查括号里的

注意:(1)单行子查询的结果不能超过1
(2)在子查询中不能使用order by子句
(3)子查询不能作为函数的参数出现
(4)子查询允许嵌套,最多可以255层

1、单行子查询(子查询的结果是一行一列,这时可以使用所有的单值运算符来连接子查询=> <>=)
eg:
①查询比20号部门平均工资高的员工信息。
select avg(sal)from emp where deptno=20;//查询员工平均工资
select *from emp where sal>(select avg(sal)from emp where deptno=20);

②把7566员工的工资改为7655员工的工资
update emp set sal=(select sal from emp where empno=7655)where empno=7566;

③把7655员工的工资信息存储到表emp1中。
create table emp1 as select sal from emp where empno=7655;
若表已存在,则:insert into emp1 select sal from emp where empno=7655;

==================================================================================

2、多行子查询(子查询的查询结果是单列多行,这时可以使用的运算符为in,any,all,
其中any,all通常与单值符结合使用)

查询与20号部门员工工资相同的职工信息
select * from emp where sal in(select sal from emp where deptno=20);

查询比20号部门任一员工工资高的职工信息
select * from emp where sal>any(select sal from emp where deptno=20);
select * from emp where sal>(select min(sal) from emp where deptno=20);

查询比20号部门所有员工工资高的职工信息
select * from emp where sal>all(select sal from emp where deptno

=20);
select * from emp where sal>(select max(sal) from emp where deptno=20);

>any>最小值
>all>最大值

==================================================================================

3、多列子查询(一般使用in或=)

找出与20 号部门工资和奖金相同的员工的工号,工资和奖金
select empno,sal,comm from emp where sal in(select sal from emp where deptno=20)and nvl(comm,0)in(select sal,nvl(comm,0)from emp where deptno=20);
select empno,sal,comm from emp where(sal,nvl(comm,0))in(select sal,nvl(comm,0)from emp
where deptno=20);

把7566员工的工资和奖金改为7655员工的工资和奖金
update emp set(sal,comm)=(select sal,comm from emp where empno=7655) where empno=7566;

4、相关子查询minus union union all intersect rownum
显示emp表的第3条记录
select * from (select rownum row1,sal,empno,hiredate from emp) r where row1=3;

显示emp表的第3-5条记录
select * from (select rownum row1,sal,empno,hiredate from emp) r where row1 between 3 and 5;

==================================================================================

11周
1、输出格式化
show all
set linesize n //每行显示的字符数
set pagesize n //每页显示的行数
set heading off/on //查询结果需要不需要输出列名
set feedback on|off //打开/关闭反馈
set echo off|on

2、在sql语句中实现选择查询
eg:10 财务部 20 研究部 30 销售部 40 管理部 其他 该部门不存在
select deptno,
decode(deptno,10,'财务部',
20,'研究部',
30,'销售部',
40,'管理部','该部门不存在')from dept;

case函数
1、简单case函数:
case expression1
when value1 then expression2
when value2 then expression3
else expressionN
end

eg:
select deptno, case deptno
when 10 then '财务部'
when 20 then '研究部'
when 30 then '销售部'
when 40 then '管理部'
else'该部门不存在'
end
from dept;

2、搜索case函数:
case
when expression1 then expression2
when expression3 then expression4
else expressionN
end
eg:
select deptno, case
when deptno=10 then '财务部'
when deptno=20 then '研究部'
when deptno=30 then '销售部'
when deptno=40 then '管理部'
else'该部门不存在'
end
from dept;

==================================================================================

查询每个员工的月收入,月收入在3000以上的显示高收入,2000到3000之间的显示中等收入,
其他显示一般收入
select sal+nvl(comm,0),case
when sal+nvl(comm,0)>3000 then'高收入'
when sal+nvl(comm,0) betwe

en 2000 and 3000 then'中等收入'
else'一般收入'
end
from emp;

==================================================================================

remark 注释:
rem"这是一个测试程序"
set heading off
set linesize 100
set pagesize 100
set feedback off
spool c:\2.txt
select * from emp;
spool off

==================================================================================
==================================================================================



相关主题
文本预览
相关文档 最新文档