Oracle数据库
1、简单介绍
Oracle服务器:是由Oracle实例和Oracle数据库组成。
Oracle实例:内存映像(内存里的数据结构)
Oracle数据库:物理里的文件集合(表等)
2、Oracle实例(两阶段提交原理)
3、基本select语句
format_model:9 数字、0 零、$ 美元符、L 本地货币符、. 小数点、,千位符
例1:查询员工的薪水:两位小数、本地货币符、千位符【转换函数】
select ename,to_char(sal,'L9,999.99') from emp;
反过来:select to_number('Y1,600.00','L9,999.99') from emp;
例2:求10号部门的平均工资【分组函数】
select deptno,avg(sal)
from emp
where deptno=10
group by deptno //若不加这句,则会把所有10号部门输出来,保证只有一个10号部门
例3:按部门统计员工人数:部门号 部门名称 部门人数【外连接】
select d.deptno,d.dname,count(e.ename)
from deptno d,emp e
where d.deptno=e.deptno (+) //左外连接:当连接条件不成立的条件下,为了显示左边有,而右边没有的信息
group by d.deptno,d.dname //d.dname必须包含在检索项中
例4:查询员工信息,老板信息【自连接】
把一张表看成两张表,自连接,员工表的老板是老板表的员工。
select e.ename || '老板是' || nvl(b.ename,'他自己') //当老板为空时,显示他自己
from emp e,emp b
where e.mgr=b.empno (+) //把员工的每一条记录显示出来,即员工老板为空时
例5:查询员工信息,和141号工种一样的,薪水比143号员工工资高的 col1,col2,col3信息【单行子查询】
select last_name,job_id,salary
from emp
where job_id=(select job_id from emp where emp_id=141)
AND salary > (select srlary from emp where emp_id=143);
例6:查询工资最低的员工信息【单行子查询】
select *
from emp
where salary = (select min(salary) from emp);
例7:求各个部门编号和部门最低工资(这个最低工资要比50号部门的最低工资要高)【单行子查询】
select dept_id,min(salary)
from emp
group by dept_id
Having min(salary)>(select min(salary) from emp where dept_id=50);
例8:查询部门名称为SALES和ACCOUNTING的员工信息【多行子查询】【in】
select *
from emp
where deptno in(select deptno from dept where dname='SALES' or dname='ACCOUNTING')
例9:查询部门名称不是SALES和ACCOUNTING的员工信息【多行子查询】【not in】
select *
from emp
where deptno not in(select deptno from dept where dname='SALES' or dname='ACCOUNTING')
例10:查询薪水比30号部门所有员工薪水高的员工信息【多行子查询】【all】
select*
from emp
where sal > all (select sal from emp where deptno=30)
=where sal > (select max(sal) from emp where deptno=30)
例11:查询薪水比30号部门任意一个员工薪高的员工信息【多行子查询】 【any】
all/any 的运算和前面的逻辑比较运算相互关联
4、面试强化
例1:查询不是经理的员工信息
select *
from emp
where empno not in (select mgr from emp where mgr is not null) //有空值
例2:找到员工表中工资最高的前三名【TOP-M问题】
select rownum,empno,ename,sal
from (select empno,ename,sal from emp order by sal desc)
where rownum<=3
//rownum:是属于服务器返回的结果集的固有属性,只能使用where rownum<=3不能where rownum>=3,因为生成结果集是1行1行生成的,没有第一行就没有第二行
例3:找到员工表中工资5~8名【分页问题】
select r,empno,sal
from
(select rownum r,empno,sal
from (select empno,ename,sal
from emp
order by sal desc)
where rownum<=8)
where r>=5 //内部是rownum不能使用>=5机制,但取别名后可以使用
解决分页问题:
总的原则:内层排序,外层选,需要三层查询
内:排序
中:使用rownum选择前n条,并给rownum指定一个别名,以供最外层过滤使用
外:去掉前m条结果
例4:找到员工表中薪水大于本部门平均薪水的员工
第一种方法:
select e.empno,e.ename,e.sal,d.avgsal
from emp e,(select deptno,avg(sal) avgsal from emp group by deptno) d
where e.deptno=d.deptno and e.sal>d.avgsal
第二种方法:相关子查询(主查询作为子查询的参数)
分析1:select empno ename,sal,(员工部门的平均工资) avgsal
from emp
where sal > (员工部门的平均工资)
分析2:员工部门的平均工资
select avg(sal) avgsal from emp where deptno=10——>10号部门的平均工资
分析3:select e.empno,e.ename,e.sal,(select avg(sal) avgsal from emp where deptno=e.deptno) avgsal
where sal>(where avg(sal) avgsal from emp where deptno=e.deptno)
总结:主查询 通过别名 把主查询的值 传递给 子查询
5、事务
一个或多个DML语句,起始标志:默认是打开的,不自动关闭,结束标志:显式提交commit,隐式提交:1)create table 2)正常退出 quit ,回滚:显式 rollback;隐式 掉电
事务特性:
- 原子性:事务中的各项操作,要么全成功,要么全失败
- 一致性:事务结后系统状态一样,比如两个账号转钱,总的金额不变
- 隔离性:两个事务之间彼此隔离
- 持久性:只要事务已提交,就不会更改
事务的隔离:多个客户端同时操作数据库时,要隔离他们的操作
否则:脏读、不可重复读、幻读
- 脏读:一个事务读取到另外一个事务没有提交的数据
- 不可重复读:在同一个事务中,两次读取到同一数据,得到结果不同,因为中间被另一个事务修改且提交了
- 幻读:在同一事务中,用同样的操作读取两次,得到的记录数不同