oracle数据库增删改查练习50-答案
一、建表--学生表 drop table student;create table student (sno varchar2(10,sname varchar2(10,sage date,ssex varchar2(10;i nsert into stude nt values('C赵雷 ',to_date('1990/01/01','yyyy/mm/dd','男';insert into student values('02'钱电 ',to_date('1990/12/21','yyyy/mm/dd'';insert into student values('03'孙风 ',to_date('1990/05/20','yyyy/mm/dd',';insert into student values('04'李云 ',to_date('1990/08/06','yyyy/mm/dd',';insert into student values('05'周梅 ',to_date('1991/12/01','yyyy/mm/dd'';insert into student values('06'吴兰 ',to_date('1992/03/01','yyyy/mm/dd'';insert into student values('07'郑竹 ',to_date('1989/07/01','yyyy/mm/dd',';insert into student values('08'王菊 ',to_date('1990/01/20','yyyy/mm/dd',';--课程表 drop table course;create table course (eno varchar2(10,cname varchar2(10,tno varchar2(10;insert into course values ('0语文 ','02';insert into course values ('02'数学','01';insert into course values ('03'英语','03';
--教师表 drop table teacher;create table teacher (tno varchar2(10,tname varchar2(10;i nsert
into teacher values('01 张三';i nsert into teacher values('02 '李四 ';insert into teacher values('03'王 五';
--成绩表 drop table sc;create table sc (sno varchar2(10,cno varchar2(10,score number(18,1;insert into sc values('01','01',80.0;insert into sc values('01','02',90.0;insert into sc values('01','03',99.0;insert into sc values('02','01',70.0;insert into sc values('02','02',60.0;insert into sc values('02','03',80.0;insert into sc values('03','01',80.0;insert into sc values('03','02',80.0;insert into sc values('03','03',80.0;insert into sc values('04','01',50.0;insert into sc values('04','02',30.0;insert into sc values('04','03',20.0;insert into sc values('05','01',76.0;insert into sc values('05','02',87.0;insert into sc values('06','01',31.0;insert into sc values('06','03',34.0;insert into sc values('07','02',89.0;insert into sc values('07','03',98.0;
commit;
二、查询1.1、查询同时存在"01"课程和"02"课程的情况select s.sno, s.sname,
s.sage, s.ssex, scl.score, sc2.score from stude nt s, sc sc1, sc sc2 where s.s no = scl.s no and s.s no = sc2.s no and sc1.c no = '01' and sc2.c no = '02';
1.2、查询必须存在"01"课程,"02"课程可以没有的情况
select t.*, s.score_01, s.score_02 from stude nt t inner join (select a.s no, a.score score_01, b.score score_02 from sc a left joi n (select * from sc where eno = '02' b on (a.s no = b.s no where a.c no = '01' s on (t.s no = s.s no;
2.1、查询同时'01'课程比'02'课程分数低的数据
select s.s no, s.s name, s.sage, s.ssex, sc1.score, sc2.score from stude nt s, sc sc1, sc
sc2 where s.s no = sc1.s no and s.s no = sc2.s no and sc1.c no = '01' and sc2.c no = '02' and sc1.score < sc2.score;
2.2、查询同时'01'课程比'02'课程分数低或'01缺考的数据select s.sno, s.sname,
s.    sage, s.ssex, t.score_01, t.score_02 from stude nt s, (select b.s no, a.score score_01,
b.score score_02 from (select * from sc where eno = '01' a, (select * from sc where eno = '02' b where a.s no(+ = b.s no t where s.s no = t.s no and (t.score_01 < t.score_02 or
t.    score_01 is nu II;
3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
select s.s no, s.s name, t.avg_score avg_score from stude nt s, (select sno, roun d(avg(score,
2 avg_score from sc group by sno hav ing avg(score >= 60 order by sno t where s.s no =
t.s no;
4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩    4.1、有基本的增删改查语句
考试成绩,且小于 60 select s.sno, s.sname, t.avg_score avg_score from student s,
(select sno, roun d(avg(score, 2 avg_score from sc group by sno hav ing avg(score < 60
order by sno t where s.s no = t.s no;
4.2、    包括没有考试成绩的数据 select g.* from (select s.sno, s.sname,
n vl(t.avg_score, 0 avg_score from stude nt s, (select sno, ro un d(avg(score, 2 avg_score from sc group by sno order by sno t where s.s no = t.s no(+ g where g.avg_score < 60;
5、 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩    5.1
查询所有成绩的(不含缺考的)。    select s.s no ,s.s unt_cno ,c.sum_scorefrom
stude nt s,(select sno ,co un t(c no count_cno ,sum(score sum_score from sc group by sno order by sno c
where s.s no = c.s no
5.2、    查询所有成绩的(包括缺考的。select a.s_sno, a.s_sname, nvl(a.c_cno, 0,
a.s_score from (select s.s no s_sno, s.s name s_sn ame, c.co unt_cno c_cno, c.sum_score s_score from stude nt s, (select sno, coun t(c no count_cno, sum(score sum_score from sc group by sno order by sno c where s.s no = c.s no(+ a

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。