資料庫精通練習題答案

嘿愛多發表於2021-01-04

CREATE TABLE students
(sno VARCHAR2(30) NOT NULL,
sname VARCHAR2(30) NOT NULL,
ssex VARCHAR2(30) NOT NULL,
sbirthday DATE,
class VARCHAR2(30));
select * from students
CREATE TABLE courses
(cno VARCHAR2(30) NOT NULL,
cname VARCHAR2(30) NOT NULL,
tno VARCHAR2(30) NOT NULL);
select * from courses
CREATE TABLE scores
(sno VARCHAR2(30) NOT NULL,
cno VARCHAR2(30) NOT NULL,
degree number(10) NOT NULL);

CREATE TABLE teachers
(tno VARCHAR2(30) NOT NULL,
tname VARCHAR2(30) NOT NULL, tsex VARCHAR2(30) NOT NULL,
tbirthday DATE NOT NULL, prof VARCHAR2(30),
depart VARCHAR2(30) NOT NULL);
select * from teachers
INSERT INTO students(SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (108 ,‘曾華’ ,‘男’ ,to_date(‘1977-09-01’, ‘yyyy-mm-dd’),95033);
INSERT INTO students(SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (105 ,‘匡明’ ,‘男’ ,to_date(‘1975-10-02’, ‘yyyy-mm-dd’),95031);
INSERT INTO students(SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (107 ,‘王麗’ ,‘女’ ,to_date(‘1976-01-23’, ‘yyyy-mm-dd’),95033);
INSERT INTO students(SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (101 ,‘李軍’ ,‘男’ ,to_date(‘1976-02-20’, ‘yyyy-mm-dd’),95033);
INSERT INTO students(SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (109 ,‘王芳’ ,‘女’ ,to_date(‘1975-02-10’, ‘yyyy-mm-dd’),95031);
INSERT INTO students(SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (103 ,‘陸君’ ,‘男’ ,to_date(‘1974-06-03’, ‘yyyy-mm-dd’),95031);

INSERT INTO courses(CNO,CNAME,TNO)VALUES (‘3-105’ ,‘計算機導論’,825);
INSERT INTO courses(CNO,CNAME,TNO)VALUES (‘3-245’ ,‘作業系統’ ,804);
INSERT INTO courses(CNO,CNAME,TNO)VALUES (‘6-166’ ,‘資料電路’ ,856);
INSERT INTO courses(CNO,CNAME,TNO)VALUES (‘9-888’ ,‘高等數學’ ,100);

INSERT INTO scores (SNO,CNO,DEGREE)VALUES (103,‘3-245’,86);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (105,‘3-245’,75);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (109,‘3-245’,68);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (103,‘3-105’,92);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (105,‘3-105’,88);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (109,‘3-105’,76);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (101,‘3-105’,64);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (107,‘3-105’,91);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (108,‘3-105’,78);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (101,‘6-166’,85);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (107,‘6-106’,79);
INSERT INTO scores (SNO,CNO,DEGREE)VALUES (108,‘6-166’,81);

INSERT INTO teachers (TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (804,‘李誠’,‘男’,to_date(‘1958-12-02’, ‘yyyy-mm-dd’),‘副教授’,‘計算機系’);
INSERT INTO teachers (TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (856,‘張旭’,‘男’,to_date(‘1969-03-12’, ‘yyyy-mm-dd’),‘講師’,‘電子工程系’);
INSERT INTO teachers (TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (825,‘王萍’,‘女’,to_date(‘1972-05-05’, ‘yyyy-mm-dd’),‘助教’,‘計算機系’);
INSERT INTO teachers (TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (831,‘劉冰’,‘女’,to_date(‘1977-08-14’, ‘yyyy-mm-dd’),‘助教’,‘電子工程系’);

select * from te
1、 查詢Students表中的所有記錄的Sname、Ssex和Class列。
select sname,ssex,class from students
2、 查詢教師所有的單位即不重複的Depart列。
select distinct depart from teachers
3、 查詢Students表的所有記錄。
select * from students
4、 查詢Scores表中成績在60到80之間的所有記錄。
select * from scores
select degree from scores where degree between 60 and 80;
5、 查詢Scores表中成績為85,86或88的記錄。
select degree from scores where degree = 85 or degree = 86 or degree = 88;
6、 查詢Students表中“95031”班或性別為“女”的同學記錄。
select * from students
select class,ssex from students where class = ‘95031’ or ssex =‘女’;
7、 以Class降序查詢Students表的所有記錄。
select class from students order by class desc;
8、 以Cno升序、Degree降序查詢Scores表的所有記錄。
select * from scores
select cno,degree from scores order by cno desc,cno asc;
9、 查詢“95031”班的學生人數。
select * from students
select count(*) from students where class =‘95031’;
10、查詢Scores表中的最高分的學生學號和課程號。

–bu
select sname from students 學號
(select max(degree) from scores)
join scores 學浩 on 學號 sno = 學浩 sno
;
11、查詢‘3-105’號課程的平均分。
select avg(degree) from scores where CNO = ‘3-105’;
12、查詢Score表中至少有5名學生選修的並以3開頭的課程的平均分數。

13、查詢最低分大於70,最高分小於90的Sno列。
select sno from scores where degree between 70 and 90;
14、查詢所有學生的Sname、Cno和Degree列。
select sname,b.cno,degree from students a join scores b on a.sno = b.sno;

15、查詢所有學生的Sno、Cname和Degree列。
select * from courses
select * from students
select * from scores

select Sno,cname,Degree from scores a join courses b on a.cno = b.cno

16、查詢所有學生的Sname、Cname和Degree列。

select sname,cname,degree from scores a, courses b, students c where a.cno = b.cno and c.sno = a.sno;

select sname,cname,degree from students t join scores s on t.sno = s.sno join courses c on s.cno = c.cno;

17、查詢“95033”班所選課程的平均分。

select avg(degree) from scores where degree in
(select degree from students a, scores c where a.sno =c.sno and class = 95033);

18、假設使用如下命令建立了一個grade表:
create table grade(low number(10),upp number(10),rank varchar2(10));
insert into grade values(90,100,‘A’);
insert into grade values(80,89,‘B’);
insert into grade values(70,79,‘C’);
insert into grade values(60,69,‘D’);
insert into grade values(0,59,‘E’);
select * from grade
現查詢所有同學的Sno、Cno和rank列。
select * from grade
select * from scores
select * from students
select sno,cno,rank from scores a join grade b on degree between low and upp;
select sno,cno,rank from scores a,grade b where degree between low and upp;
19、查詢選修“3-105”課程的成績高於“109”號同學成績的所有同學的記錄。

select * from scores where degree >all (select degree from scores where sno=109) and cno = ‘3-105’

select * from scores where degree >
(select degree from scores where cno ='3-105’and sno = 109) and cno = ‘3-105’

20、查詢score中選學一門以上課程的同學中分數為非最高分成績的記錄。
(select * from scores a join students b on a.sno = b.sno)
select * from students

select * from scores where degree not in(
select max(degree) from scores group by sno having count(sno) >1);

select degree from scores where degree not in
(select max(degree) from scores group by sno having count (sno) >1)
21、查詢成績高於學號為“109”、課程號為“3-105”的成績的所有記錄。
select * from scores where degree >all
(select degree from scores where cno = ‘3-105’ and sno = 109) and cno = ‘3-105’;

22、查詢“張旭“教師任課的學生成績。
select * from teachers
select * from students
select * from scores
select * from courses
select * from scores a where a.cno in(
select cno from courses where tno in(
select tno from teachers where tname = ‘張旭’));

23、查詢選修某課程的同學人數多於5人的教師姓名。

select tname from teachers t join courses c on t.tno = c.tno where cno =
(select cno from scores group by cno having count(cno)>5)

24、查詢95033班和95031班全體學生的記錄。
select * from students
select class
select * from students where class =any
(select * from students where class in (95033 ,95031) );
25、查詢存在有85分以上成績的課程Cno.
select distinct cno from scores where degree > 85
26、查詢出“計算機系“教師所教課程的成績表。
select * from scores where cno in(
select cno from courses where tno in
(select tno from teachers where depart = ‘計算機系’));
select * from scores where cno in
(select cno from teachers a,courses b where a.tno = b.tno and depart = ‘計算機系’);
27、查詢“計算機系”與“電子工程系“不同職稱的教師的Tname和Prof。

select * from teachers where prof in
(select prof from teachers group by prof having count(prof)=1)and depart in( ‘計算機系’ ,‘電子工程系’);

28、查詢選修編號為“3-105“課程且成績至少高於選修編號為“3-245”的同學的Cno、Sno和Degree,並按Degree從高到低次序排序。

select * from courses where cno = ‘3-105’;
select * from scores where degree > any
(select degree from scores where cno = ‘3-105’)and cno =‘3-245’ order by degree desc;

29、查詢選修編號為“3-105”且成績高於選修編號為“3-245”課程的同學的Cno、Sno和Degree.
select * from courses where cno = ‘3-105’
select * from scores where degree >all
(select degree from scores where cno = ‘3-245’)and cno =‘3-105’;

30、查詢所有教師和同學的name、sex和birthday.
select tname,tsex,tbirthday from teachers
union
select sname,ssex,sbirthday from students

31、查詢所有“女”教師和“女”同學的name、sex和birthday.
select tname,tsex,tbirthday from teachers where tsex = ‘女’
union
select sname,ssex,sbirthday from students where ssex = ‘女’;
32、查詢成績比該課程平均成績低的同學的成績表。
select * from scores where
(select avg(degree) from scores );

select * from scores c where degree < (
select avg(degree) from scores e group by cno having e.cno = c.cno);

select * from scores;
33、查詢所有任課教師的Tname和Depart.
34 查詢所有未講課的教師的Tname和Depart.
35、查詢至少有2名男生的班號。
select distinct class frjava sources om students where class in
(select class from students group by class having count (ssex)>1 ) and ssex=‘男’ ;
36、查詢Student表中不姓“王”的同學記錄。
select sname from students where sname not like ‘王%’;
37、查詢Student表中最大和最小的Sbirthday日期值。
38、以班號和年齡從大到小的順序查詢Student表中的全部記錄。
39、查詢“男”教師及其所上的課程。
40、查詢最高分同學的Sno、Cno和Degree列。
select sno,cno,degree from scores where degree in
(select max(degree) from scores);
41、查詢和“李軍”同性別的所有同學的Sname.
42、查詢和“李軍”同性別並同班的同學Sname.
select sname from students where ssex in
(select ssex from students where sname = ‘李軍’)
and class in
(select class from students where sname = ‘李軍’)
and sname != ‘李軍’;

select * from students where class in(
select class from students where ssex =
(select ssex from students where sname = ‘李軍’ ))and sname = ‘李軍’;
43、查詢所有選修“計算機導論”課程的“男”同學的成績表

select ssex, o.cno,o.sno,degree from
students s ,scores o,courses c where s.sno = o.sno
and o.cno = c.cno and cname = ‘計算機導論’ and ssex = ‘男’;

相關文章