Oracle数据库笔记06

Oracle数据库笔记06

–多表查询

查询每个员工的上级领导

select e.empno,e.ename,e1.empno,e1.ename from emp e,emp e1 where e.mgr = e1.empno;

–查询出每个员工的员工编号、姓名、部门名称、工资等级和他的上级领导的姓名、工资等级

select * from emp;
select e.empno,e.ename,d.dname,decode(s.grade,1,'一级',2,'二级',3,'三级',4,'四级',5,'五级','没级'),e1.ename,decode(s1.grade,1,'一级',2,'二级',3,'三级',4,'四级',5,'五级','没级')
from emp e,dept d,salgrade s,emp e1,salgrade s1
where E.DEPTNO = D.DEPTNO and e.sal BETWEEN s.losal and s.hisal and e.mgr = e1.empno and e1.sal BETWEEN s1.losal and s1.hisal;

–查询每个部门的员工,

两张表做连接查询时其中一张表要查询全量的数据(不会因为另一张表的数据关联而被筛选掉)

在两张表关联的时候费全量表的关联字段后面加上(+)就可以做外连接查询

SELECT * FROM dept d,emp e where d.deptno = e.deptno;
SELECT * FROM dept d,emp e where d.deptno = e.deptno(+);
--查询员工的编号和姓名和他的领导的编号和姓名
select e.empno,e.ename,e1.empno,e1.ename from emp e,emp e1 where e.mgr = e1.empno(+);

–分组函数

SELECT * FROM emp;
SELECT count(*) FROM emp;
SELECT count(empno) FROM emp;

–查询员工最低工资

SELECT min(sal) FROM emp;–最低
SELECT max(sal) FROM emp;–最高
SELECT avg(sal) FROM emp;–平均
SELECT trunc(avg(sal)) FROM emp;–平均工资取整

–查询出20部门的员工的工资总和

select sum(sal) from emp where deptno = 20;–求和
select deptno,count(empno) from emp group by deptno;
select deptno,avg(sal) from emp group by deptno;

–查询出部门平均工资大于2000的部门

select avg(e.sal),e.deptno from emp e group by e.deptno having avg(e.sal)>2000;

–显示非销售人员工作名称一级从事同一工作的员工的月工资总和,并且要求满足从事同一工作月工资总和大于5000,结果按月工资总和的升序排列

select e.job from emp e where e.job != ‘SALESMAN’;–10个 显示非销售人员工作名称
select e.job,sum(e.sal) from emp e where e.job != ‘SALESMAN’ group by e.job having sum(e.sal) > 5000;
select e.job,sum(e.sal) from emp e where e.job != ‘SALESMAN’ group by e.job having sum(e.sal) > 5000 order by e.job asc;

–子查询

查询比7654工高的雇员

select sal from emp where empno = 7654;
select e1.* from emp e1 where e1.sal > (select sal from emp where empno = 7654);

–查询出比雇员7654的工资高,同时从事和7788的工作一样的员工

select e1.* from emp e1 where e1.sal > (select sal from emp where empno = 7654) and e1.job = (select job from emp where empno = 7788);

–要求查询每个部门的最低工资和最低工资的雇员和部门名称

select e1.deptno,min(e1.sal) from emp e1
group by e1.deptno;–查询每个部门的最低工资
select e.,d.
from emp e,(select e1.deptno,min(e1.sal) minsal from emp e1 group by e1.deptno) biao,dept d
where e.sal = biao.minsal and e.deptno = biao.deptno and e.deptno = d.deptno;

–查询每个部门最低员工的工资

select * from emp e1 where e1.sal in (select min(e.sal) from emp e group by e.deptno);

–查询出所有员工的部门有哪些

select * from dept e where e.deptno in (select distinct deptno from emp);

–少用in性能低,可以使用exist

select * from dept t where exists (select * from emp e where e.deptno = t.deptno);

–UNION 并集 会自动去掉重复的项 合并的时候必须保证一一对应类型一致 并集的列数必须一致

select * from emp where sal > 1000
union
select * from emp where sal > 2000;

–UNION ALL 并集 不会去掉重复的项

select * from emp where sal > 1000
union all
select * from emp where sal > 2000;

–复制一张表作为备份,练习增删改查

create table myemp as select * from emp;–复制表
–增
insert into myemp(empno,ename,job,mgr,hiredate,sal,comm,deptno)
values(8000,‘keke’,‘sport’,7369,to_date(‘2000-11-16’,‘yyyy-mm-dd’),1000,200,10);
select * from myemp;

–改

update myemp t set t.ename = ‘xiaozhang’,t.sal = 1213 where t.empno = 8000;

–删除

delete from myemp t where t.empno = 8000;

–事务

select * from myemp;

–增删改都要开启事务

事务在没提交之前都可以回滚,但是提交了以后就不能回滚了

commit;
rollback;

–建表

create table person(
person_id number(4),
pname varchar2(10),
age number(3),
birthday date
);
insert into person(person_id,pname,age,birthday) values(8001,‘baobao’,33,to_date(‘1997-07-22’,‘yyyy-mm-dd’));

–删表

drop table person;

–添加列

alter table person add(address varchar2(50));

–修改列的属性

alter table person modify(address varchar2(12));

commit;

–检查约束

drop table person;
create table person(
person_id number(4),
pname varchar2(10),
gender number(1) check(gender in (1,2)),
birthday date
);
insert into person(person_id,pname,gender,birthday) values(8001,‘baobao’,2,to_date(‘1997-07-22’,‘yyyy-mm-dd’));

–有外键关联的表强行删除主表

drop table orders cascade constraint;

–外键约束标准格式

drop table orders;
create table orders(
order_id number(4),
totalprice number(7,2),
order_time date,
constraint orders_order_id_pk primary key(order_id)
);
drop table order_detail;
create table order_detail(
detail_id number(4),
item_name varchar2(10),
price number(7,2),
quantity number(4),
order_id number(4),
constraint order_detail_id_pk primary key(detail_id),
constraint order_detail_id_fk foreign key(order_id)
references orders(order_id) on delete cascade --级联删除 这样删除表的时候就不用管子表了,主表记录删了,子表表跟着就删了
);

–插入数据

insert into orders values(1000,10000,sysdate);
insert into order_detail values(1000,‘HUAWEI’,5000,2,1000);

你可能感兴趣的:(oracle)