test.sql文件
prompt PL/SQL Developer import file
prompt Created on 2018年11月29日 by my
set feedback off
set define off
prompt Creating COURSE...
create table COURSE
(
cno VARCHAR2(5) not null,
cname VARCHAR2(10) not null,
tno VARCHAR2(10) not null
)
;
prompt Creating SCORE...
create table SCORE
(
sno VARCHAR2(3) not null,
cno VARCHAR2(5) not null,
degree NUMBER not null
)
;
prompt Creating STUDENT...
create table STUDENT
(
sno VARCHAR2(3) not null,
sname VARCHAR2(4) not null,
ssex VARCHAR2(2) not null,
sbirthday DATE,
class NUMBER not null
)
;
prompt Creating TEACHER...
create table TEACHER
(
tno VARCHAR2(3) not null,
tname VARCHAR2(4) not null,
tsex VARCHAR2(2) not null,
tbirthday DATE not null,
prof VARCHAR2(6),
depart VARCHAR2(10) not null
)
;
prompt Loading COURSE...
insert into COURSE (cno, cname, tno)
values ('3-105', '计算机导论', '825');
insert into COURSE (cno, cname, tno)
values ('3-245', '操作系统', '804');
insert into COURSE (cno, cname, tno)
values ('6-166', '数据电路', '856');
insert into COURSE (cno, cname, tno)
values ('9-888', '高等数学', '100');
commit;
prompt 4 records loaded
prompt Loading SCORE...
insert into SCORE (sno, cno, degree)
values ('103', '3-245', 86);
insert into SCORE (sno, cno, degree)
values ('105', '3-245', 75);
insert into SCORE (sno, cno, degree)
values ('109', '3-245', 68);
insert into SCORE (sno, cno, degree)
values ('103', '3-105', 92);
insert into SCORE (sno, cno, degree)
values ('105', '3-105', 88);
insert into SCORE (sno, cno, degree)
values ('109', '3-105', 76);
insert into SCORE (sno, cno, degree)
values ('101', '3-105', 64);
insert into SCORE (sno, cno, degree)
values ('107', '3-105', 91);
insert into SCORE (sno, cno, degree)
values ('108', '3-105', 76);
insert into SCORE (sno, cno, degree)
values ('101', '6-166', 85);
insert into SCORE (sno, cno, degree)
values ('107', '6-166', 79);
insert into SCORE (sno, cno, degree)
values ('108', '6-166', 81);
insert into SCORE (sno, cno, degree)
values ('101', '9-888', 90);
insert into SCORE (sno, cno, degree)
values ('101', '3-245', 88);
commit;
prompt 14 records loaded
prompt Loading STUDENT...
insert into STUDENT (sno, sname, ssex, sbirthday, class)
values ('133', '乔安', '女', to_date('15-05-1996', 'dd-mm-yyyy'), 95033);
insert into STUDENT (sno, sname, ssex, sbirthday, class)
values ('108', '曾华', '男', to_date('01-09-1977', 'dd-mm-yyyy'), 95033);
insert into STUDENT (sno, sname, ssex, sbirthday, class)
values ('105', '匡明', '男', to_date('02-10-1975', 'dd-mm-yyyy'), 95031);
insert into STUDENT (sno, sname, ssex, sbirthday, class)
values ('107', '王丽', '女', to_date('23-01-1976', 'dd-mm-yyyy'), 95033);
insert into STUDENT (sno, sname, ssex, sbirthday, class)
values ('101', '李军', '男', to_date('20-02-1976', 'dd-mm-yyyy'), 95033);
insert into STUDENT (sno, sname, ssex, sbirthday, class)
values ('109', '王芳', '女', to_date('10-02-1975', 'dd-mm-yyyy'), 95031);
insert into STUDENT (sno, sname, ssex, sbirthday, class)
values ('103', '陆君', '男', to_date('03-06-1974', 'dd-mm-yyyy'), 95031);
commit;
prompt 7 records loaded
prompt Loading TEACHER...
insert into TEACHER (tno, tname, tsex, tbirthday, prof, depart)
values ('804', '李诚', '男', to_date('02-12-1978', 'dd-mm-yyyy'), '副教授', '计算机系');
insert into TEACHER (tno, tname, tsex, tbirthday, prof, depart)
values ('856', '张旭', '男', to_date('12-03-1979', 'dd-mm-yyyy'), '讲师', '电子工程系');
insert into TEACHER (tno, tname, tsex, tbirthday, prof, depart)
values ('825', '王萍', '女', to_date('05-05-1982', 'dd-mm-yyyy'), '助教', '计算机系');
insert into TEACHER (tno, tname, tsex, tbirthday, prof, depart)
values ('831', '刘冰', '女', to_date('14-08-1987', 'dd-mm-yyyy'), '助教', '电子工程系');
commit;
prompt 4 records loaded
set feedback on
set define on
prompt Done.
1.用语句新建用户名为test 密码为test 的用户,赋权resource connect,删除用户test
create user test identifity by test;Grant resource,connect to test;drop user test cascade;
2.导入表结构及数据。
在PL/SQL中导入.sql文件。工具——》导入表——》SQL插入——》使用命令窗口
3.查询Score表中成绩为85,86或88的记录
select * from score where degree in(85,86,88);
4.查询Student表中“95031”班或性别为“女”的同学记录。
SELECT * FROM STUDENT WHERE CLASS='95031' OR SSEX='女';
5.以Class降序查询Student表的所有记录。
select * from student order by class desc;
6.查询Score表中的最高分的学生学号和课程号。
select sno,cno from score where degree in (select max(degree) from score);
7.查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
select avg(degree) from score where cno like '3%' and cno in (select cno from score group by cno having count(cno)>=5);
8.查询选修编号为“3-105“且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
select cno,sno,degree from score where cno = '3-105' and degree >(select max(degree) from score where cno = '3-245') order by degree desc;
9.备份表COURSE,命名为COURSE1,其中不包含课程名为高等数学的记录
create table course2 as select * from course where cname<>'高等数学';
10.查询COURSE1基于COURSE变化的数据
11.查询课程号为‘3-105’的学生学号、姓名、成绩和排名,成绩相同时并列,后续排名空出(如:1,2,3,3,5)
select sno,cno,degree,rank() over( order by degree desc) rank from score where cno='3-105' order by rank,sno;
12.查询课程号为‘3-105’的学生学号、姓名、成绩和排名,成绩相同时并列,后续排名接上(如:1,2,3,3,4)
select sno,cno,degree,dense_rank() over(order by degree desc) rank from score where cno='3-105' order by rank;
13.查询所有学生的计算机导论成绩,不能用子查询
select degree from score a, course b where a.cno=b.cno and b.cname='计算机导论';
14.查询选修课的任课教师的职称为副教授的学生学号
select a.sno from student a,score b where a.sno=b.sno and b.cno in (select cno from course where tno in(select tno from teacher where prof='副教授'));
15.统计每个班的学生人数(列出班级编号和人数)
select class,count(sno) from student group by class;
16.查询年龄大于35岁的教师信息
select * from teacher where to_char(sysdate,'yyyy')-to_char(tbirthday,'yyyy')>35;
sysdate:系统时间。已知日期字段计算年龄时,可以使用字符型类型转换
17.查询男学生和男老师的姓名,性别,生日
select sname as name,ssex as sex ,sbirthday as birthday from student where ssex='男' union select tname as name ,tsex as sex,tbirthday as birthday from teacher where tsex='男';
18.查询“3-245”课程比“6-166”课程成绩高的所有学生的学号
select sno from score where cno='3-245' and degree >(select max(degree) from score where cno='6-166');
19.查询平均成绩大于60分的同学的学号和平均成绩
select sno,avg(degree) from score where sno in(select sno from score group by sno having avg(degree)>60) group by sno;
20.查询每门课成绩最高的学生学号和姓名