--一、现有学生表student,班级表classInfo,表结构如下:
--student表:sid学号,sname姓名,sex性别,birthday生日,age入学年龄,smoney缴费,cid班级ID
--classInfo表:班级编号cid,班级名称cname
--建表
drop table student cascade constraint;
drop table classinfo cascade constraint;
create table student(
sid number(6) constraint pk_stu_sid primary key,
sname varchar2(20),
sex varchar2(4),
birthday date,
age number(2),
smoney number(7,2),
cid number(4)
);
create table classinfo(
cid number(4) constraint pk_cls_cid primary key,
cname varchar2(20)
);
alter table student add constraint fk_stu_cls_cid foreign key(cid) references classinfo(cid);
insert into classinfo values(1101,'工商管理');
insert into classinfo values(1102,'计算机');
insert into classinfo values(1103,'会计');
insert into classinfo values(1104,'药品');
insert into student values(110001,'小明','男',to_date('1999-9-9','yyyy-mm-dd'),10,11220.50,1103);
insert into student values(110002,'小张','女',to_date('1991-6-9','yyyy-mm-dd'),19,10000.00,1102);
insert into student values(110003,'小红','女',to_date('1996-7-3','yyyy-mm-dd'),14,8800.00,1102);
insert into student values(110004,'张三','男',to_date('1996-2-1','yyyy-mm-dd'),14,6600.00,1102);
insert into student values(110005,'小花','女',to_date('1994-6-7','yyyy-mm-dd'),16,11440.50,1101);
insert into student values(110006,'王五',null,to_date('1991-12-5','yyyy-mm-dd'),20,11440.50,1101);
commit;
select * from student; 
select * from classinfo;
--1、 查询入学年龄在18-20的女生或者未输入性别的学生信息,且年龄小的排在后面。
select * from student where age between 18 and 20 and (sex='女' or sex is null);
--2、 查询班级名称、学生姓名、性别、缴费(要求显示单位:元),相同班级的要放在一起,再按姓名升序排列。
select c.cname,s.sname,s.sex,s.smoney||'元' smoney from student s
join classinfo c on s.cid=c.cid order by c.cname,s.sname;
--3、 查询各班名称和人数。
select cname,count(*) from classinfo c join student s on c.cid=s.cid group by cname;
--4、 查询各班名称和人数,但人数必须不少于2,且人数多的放在前面。
select cname,count(*) from classinfo c join student s on c.cid=s.cid 
group by cname having count(*)>=2 order by count(*) desc;
--5、 查询1980年出生的有哪些学生。
select * from student where to_char(birthday,'yyyy')='1980';
select * from student where to_char(birthday,'yyyy')='1991';
--6、 查询男生和女生人数,没有输入性别的当作男生计算。
select sex,count(*) from (select nvl(sex,'男') sex from student) group by sex;
--7、 查询没有人员的班级。
select * from classinfo where cid not in(select distinct cid from student);
--8、 查询入学年龄在20以上的学生信息。
select * from student where age>20;
select * from student where age>=20;
--9、 查询班级平均入学年龄在20及以上的班级名称和平均年龄。
select * from classinfo c join (select cid,avg(age) avg_age from student group by cid) t 
on c.cid=t.cid where avg_age>=20;
select * from classinfo c join (select cid,avg(age) avg_age from student group by cid) t 
on c.cid=t.cid where avg_age>=15;
--二、现有 
--部门表bm:bid部门编号,bname名称
--人员表ry:rid人员编号,rname名称,bid部门编号
--工资表gz:rid人员编号,sal工资金额,rq发放日期
--建表
drop table bm cascade constraint;
drop table ry cascade constraint;
drop table gz cascade constraint;
create table bm(
bid number(4) constraint pk_bm_bid primary key,
bname varchar2(10)
);
create table ry(
rid number(4) constraint pk_ry_rid primary key,
rname varchar2(20),
bid number(4)
);
create table gz(
rid number(4),
sal number(7,2),
rq date
);
alter table ry add constraint fk_ry_bm_bid foreign key(bid) references bm(bid);
alter table gz add constraint fk_gz_ry_rid foreign key(rid) references ry(rid);
insert into bm values(1001,'销售');
insert into bm values(1002,'后勤');
insert into ry values(1101,'tom',1001);
insert into ry values(1103,'barbie',1001);
insert into ry values(1102,'jim',1002);
insert into gz values(1101,1200.5,sysdate);
insert into gz values(1101,1200.5,sysdate);
insert into gz values(1102,2000,sysdate);
insert into gz values(1103,5000,sysdate);
select * from bm;
select * from ry;
select * from gz;
--1、 查询员工姓名、部门名称和个人总工资。
select r.rname,b.bname,g.sal from bm b join ry r on b.bid=r.bid
join (select rid,sum(sal) sal from gz group by rid) g on g.rid=r.rid;
--2、 查询本月发了2笔以上工资的员工信息。
select * from ry r join 
(select rid,to_char(rq,'yyyy-mm') rq,count(sal) sals from gz group by rid,rq) t
on r.rid=t.rid where t.rq=to_char(sysdate,'yyyy-mm') and sals=2;
--3、 查询各部门的总工资。ry.bid group by bm.bname;
select bname,sum(sal) from bm b join ry r on b.bid=r.bid join gz g on r.rid=g.rid group by bname;
--4、 查询2009年8月份各部门工资最高的员工信息,显示部门名称、员工姓名和员工工资。
select * from
(select b.bid,max(g.sal) sal from bm b join ry r on b.bid=r.bid
join (select rid,to_char(rq,'yyyy-mm') rq,sum(sal) sal from gz group by rid,rq) g on g.rid=r.rid 
group by b.bid,b.bname) t1
join 
(select b.bid,b.bname,r.rid,r.rname,g.sal from bm b join ry r on b.bid=r.bid
join (select rid,to_char(rq,'yyyy-mm') rq,sum(sal) sal from gz group by rid,rq) g on g.rid=r.rid) t2
on
t1.bid = t2.bid and t1.sal=t2.sal;
--一、现有学生表student,班级表classInfo,表结构如下:
--student表:sid学号,sname姓名,sex性别,birthday生日,age入学年龄,smoney缴费,cid班级ID
--classInfo表:班级编号cid,班级名称cname
--1、 把新进的学生Tom,其学号为x009,年龄19,分配到5班。
--insert into student values(009,'tom',null,null,19,null,5);
insert into student values(009,'tom',null,null,19,null,1103);
--2、 把1班的前5个人分到2班。
select * from student;
update student set cid = 1102 
where sid in (select * from (select sid from student where cid=1101 order by sid) where rownum<=5);
select * from student;
--3、 把学生需要的缴费降低5%。
select * from student;
update student set smoney = smoney*0.95;
select * from student;
--4、 删除年龄不满18岁的学生信息。
select * from student;
delete from student where age < 15;
select * from student;
--5、 查询1990年以前出生的有哪些学生。
select * from student where to_char(birthday,'yyyy')<1990;
select * from student where to_char(birthday,'yyyy')<2000;
--6、 查询班级平均入学年龄在20及以上的班级名称和平均年龄。
select * from classinfo c join
(select cid,avg(age) from student group by cid) t
on c.cid=t.cid;
--二、现有
--部门表bm:bid部门编号,bname名称
--人员表ry:rid人员编号,rname名称,bid部门编号
--工资表gz:rid人员编号,sal工资金额,rq发放日期
--要求:用显式事务实现给销售部中工资>5000的员工降薪5%,同时,工资<1000的员工涨薪10%。
--select * from gz;
--update gz set sal=sal*0.95 where rid in (select r.rid from bm b join ry r on b.bid=r.bid join gz g on g.rid=r.rid where sal >2000);
--update gz set sal=sal*1.1 where rid in (select r.rid from bm b join ry r on b.bid=r.bid join gz g on g.rid=r.rid where sal <2000);
--select * from gz;
declare
cursor c is
select b.bid, r.rid, g.sal
from bm b
join ry r on b.bid = r.bid
join gz g on g.rid = r.rid;
v_temp c%rowtype;
begin
open c;
loop
fetch c
into v_temp;
exit when(c%notfound);
if (v_temp.sal > 5000) then
update gz set sal = sal * 0.95 where rid = v_temp.rid;
commit;
dbms_output.put_line('工资大于5000降薪5%--' || v_temp.sal);
elsif (v_temp.sal < 1000) then
update gz set sal = sal * 1.1 where rid = v_temp.rid;
commit;
dbms_output.put_line('工资小于1000涨薪10%--' || v_temp.sal);
end if;
end loop;
close c;
end;

Oracle 练习作业10.1-1-2相关推荐

  1. 大数据预科班作业10

    大数据预科班作业10 1. 填空 Collection 接口的特点是元素是_________:List 接口的特点是元素_____(有 |无)顺序,______(可以|不可以)重复: Set 接口的特 ...

  2. oracle 取前10条记录

    1.oracle 取前10条记录 1) select * from tbname where rownum < 11; 2) select * from (select * from tbnam ...

  3. maven中引入oracle驱动报错Missing artifact com.oracle:ojdbc14:jar:10.2.0.4.0

    maven中引入oracle驱动报错Missing artifact com.oracle:ojdbc14:jar:10.2.0.4.0 问题:引入依赖之后会报错.在maven中央库中查找ojdbc, ...

  4. 优化Oracle数据库查询10个方法

    优化Oracle数据库查询10个方法: 网页制作中数据的查询是最主要的功能之一,其性能的好坏直接关系到网页的的运行效率.下面对Oracle数据库中大家不太注意的查询方法做一些总结. 第一个方法:利用连 ...

  5. 3D游戏编程与设计作业10

    3D游戏编程与设计作业10 环境说明 Unity3D 导航与寻路 Agent 和 Navmesh 练习 Obstacle和Off-Mesh-Link练习 P&D 过河游戏智能帮助实现 状态图 ...

  6. 怎么看oracle定时作业,Oracle 定时作业Job详解

    Oracle 定时作业Job :用于安排和管理作业队列,通过使用作业,可以使Oracle数据库定期执行特定的任务.而且Oracle重新启动后,Job会继续运行,不用重新启动. 场景: (1)后台业务统 ...

  7. oracle10漏洞修复,Oracle 2009年10月紧急补丁更新修复多个漏洞

    安全漏洞:CN-VA09-94 发布日期:2009年10月22日 漏洞类型:远程执行代码.拒绝服务 漏洞评估:严重 受影响的软件: * Oracle Database 11g, version 11. ...

  8. Oracle Application Server 10.1.3.0 for IBM X346 (Linux X86)安装手册

    一.安装准备 (一)  检测硬件需求 CPU :至少Pentium 450 MHZ 执行以下命令来检测: [root@localhost /]# grep MemTotal /proc/meminfo ...

  9. ORACLE RAC 从10.2.0.5.0升级到10.2.0.5.8

    一.准备工作 需要 p13923855_102058_Linux-x86-64.zip p6880880_102000_Linux-x86-64.zip 首先升级OPatch,解压OPatch unz ...

最新文章

  1. iOS App 连接外设的几种方式
  2. PaddlePaddle实现波士顿房价预测
  3. ipad iphone开发_如何将iPhone或iPad置于恢复模式
  4. Wijmo 更优美的jQuery UI部件集:复合图表(CompositeChart)
  5. 【蓝桥杯 - 真题】六角幻方(dfs+剪枝)
  6. js导出的xlsx无法打开_js-xlsx 导出表格至excel
  7. 栈的应用 - 就近匹配
  8. Python菜鸟入门:day05列表
  9. 企业微信接收消息服务器配置php,微信企业号配置及在公众号里获取用户信息
  10. HDU2000 ASCII码排序【字符串排序】
  11. SQL:集成 SQL Server 2008 R2 SP3
  12. java ts视频文件合并
  13. 浅分享一下zzulioj刷题总结
  14. MSYS以及MinGW安装
  15. 陶哲轩实分析 3.2 节 习题试解
  16. Python 斐波那契数列 及 杨辉三角
  17. Java中正负数二进制表示
  18. PC上网页端屏蔽知乎上的视频
  19. 计算机表格a列除b列,win10系统下EXCEL表格A列B列C列消失了怎么办
  20. 科技巨头Software AG遭攻击,关闭445高危端口后,Windows共享用不了,怎么办?

热门文章

  1. IIS 5 与IIS 6 原理介绍
  2. spring boot 1.4默认使用 hibernate validator
  3. js客户端存储之Web存储
  4. nginx(五)rewrite
  5. Tomcat版本与Servlet、JSP等版本的支持关系
  6. SQL调用C# dll(第一中DLL,没使用强名称密匙,默认是 safe)
  7. MySQL replace into (insert into 的增强版)
  8. Hanoi(汉诺)塔问题
  9. SMTP 错误代码大全
  10. JSON为什么那样红(另有洞天)