建表:
/***学生表********************************************************/
create student( //创建学生表
sno varchar2(10) primary key,
sname varchar2(2),
sage number(2),
ssex varchar2(5)
);
insert into student values ('s001','张三',23,'男');
insert into student values ('s002','李四',23,'男');
insert into student values ('s003','吴鹏',25,'男');
insert into student values ('s004','琴沁',20,'女');
insert into student values ('s005','王丽',20,'女');
insert into student values ('s006','李波',21,'男');
insert into student values ('s007','刘玉',21,'男');
insert into student values ('s008','萧蓉',21,'女');
insert into student values ('s009','陈萧晓',23,'女');
insert into student values ('s010','陈美',22,'女');
commit;
/*****教师表*************************************************************/
create teacher(//老师表
tno varchar2(10) primary key,
tname varchar2(20)
);
insert into teacher values ('t001', '刘阳');
insert into teacher values ('t002', '谌燕');
insert into teacher values ('t003', '胡明星');
commit;
/****课程表******************************************************************/
create course(//课程表
cno varchar2(10),//课程no
cname varchar2(20),//课程名
tno varchar2(20),//老师no
contraint pc_course primary key(cno,tno)
);
insert into course values ('c001','J2SE','t002');
insert into course values ('c002','Java Web','t002');
insert into course values ('c003','SSH','t001');
insert into course values ('c004','Oracle','t001');
insert into course values ('c005','SQL SERVER 2005','t003');
insert into course values ('c006','C#','t003');
insert into course values ('c007','JavaScript','t002');
insert into course values ('c008','DIV+CSS','t001');
insert into course values ('c009','PHP','t003');
insert into course values ('c010','EJB3.0','t002');
commit;
/***成绩表*************************************************************/
create score(//分数表
sno varchar2(10),//学生no
cno varchar2(10),//课程no
score number(4,2),//分数
constraint pk_sc primary key(sno,cno)
);
insert into sc values ('s001','c001',78.9);
insert into sc values ('s002','c001',80.9);
insert into sc values ('s003','c001',81.9);
insert into sc values ('s004','c001',60.9);
insert into sc values ('s001','c002',82.9);
insert into sc values ('s002','c002',72.9);
insert into sc values ('s003','c002',81.9);
insert into sc values ('s001','c003','59');
commit;
/******************************************************************************************************************/
练习:
注意:以下练习中的数据是根据初始化到数据库中的数据来写的SQL 语句,请大家务必注意。
1、查询“c001”课程比“c002”课程成绩高的所有学生的学号;
步骤:(1)select * from score where cno='c001';//c001分数分布,(分数表字段:学生no,课程no,分数)
(2)select * from score where cno='c002';//c002分数分布
(3)select a.sno from
(select * from score where a.cno='c001')a,//c001分数分布
(select * from score where a.cno='c002')b,//c002分数分布
where a.sno=b.sno //用sno关联
and a.score>b.score;
用exists:
select a.sno from score a
where a.cno='c001'
and exists(select * from score b where b.sno=a.sno and a.score>b.score);
/******************************************************************************************************************/
2、查询平均成绩大于60 分的同学的学号和平均成绩;
(1) select average(score) from score group by sno;//首先求平均成绩
(2)select sno,average(score) as from score group by sno where as>60;//求平均数avg,group by 和having一起用
select sno,avg(score) from sc group by sno having avg(score)>60;
/******************************************************************************************************************/
3、查询所有同学的学号、姓名、选课数、总成绩;
(1)选课数和总成绩:select count(cno),sum(score) from score group by sno;
(2)select st.sno,st.sname, count(cno),sum(score) from score sc,student st where sc.sno=st.sno group by sno;
select s.sno,s.sname,a.count,a.sumc
from (select sno, count(cno) count,sum(score) sumc from score group by sno) a,sutdent s
where a.sno=s.sno;
/******************************************************************************************************************/
4、查询姓“刘”的老师的个数;
(1)select * from teacher where tname like '刘%';
(2)select count(*) from teacher where tname like '刘%';
/******************************************************************************************************************/
5、查询没学过“谌燕”老师课的同学的学号、姓名;
(1)select c.cno from course c,teacher t where c.tno=t.tno and t.tname='谌燕'; //查询谌燕的课
(2)select st.sno,st,sname from score s,student st
where s.sno=st.sno
and s.cno not exists (select c.cno from course c,teacher t where c.tno=t.tno and t.tname='谌燕');
用join:
select * from student st where st.sno not in
(select distinct sno from sc s join course c on s.cno=c.cnojoin teacher t on c.tno=t.tno where tname='谌燕');
/******************************************************************************************************************/
6、查询学过“c001”并且也学过编号“c002”课程的同学的学号、姓名;
(1)select s.sno from score s where s.cno='c001';
(2) select s.sno from score s where s.cno='c002';
(3)slect st.sno,st,sname from student st
where st.sno in (select s.sno from score s where s.cno='c001')
and st.sno in (select s.sno from score s where s.cno='c002');
用join:
select st.* from sc a
join sc b on a.sno=b.sno
join student st
on st.sno=a.sno
where a.cno='c001' and b.cno='c002' and st.sno=a.sno;
/******************************************************************************************************************/
7、查询学过“谌燕”老师所教的所有课的同学的学号、姓名;????????????
(1)select c.cno from course c teacher t where c.tno=t.tno and t.tname='谌燕';//查询谌燕的课
(2)select st.* from student st join sc s on st.sno=s.sno
join course c on s.cno=c.cno
join teacher t on c.tno=t.tno
where t.tname='谌燕' ;//查询上过谌燕的课的所有学生
/******************************************************************************************************************/
9、查询所有课程成绩小于60 分的同学的学号、姓名;
(1)select * from score s where s.score<60;//60分以下的分数
(2)
https://www.2cto.com/database/201203/121949.html