码迷,mamicode.com
首页 > 数据库 > 详细

数据库练习题

时间:2018-07-13 13:20:49      阅读:37      评论:0      收藏:0      [点我收藏+]

标签:des   数字   gre   esc   答案   子查询   date   nbsp   ike   

题目

一、            设有一数据库,包括四个表:学生表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)。四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1-2的表(一)~表(四)所示。用SQL语句创建四个表并完成相关题目。

          表1-1数据库的表结构 

    表(一)Student (学生use)                         

属性名

数据类型

可否为空

含 义

Sno

varchar (20)

学号(主码)

Sname

varchar (20)

学生姓名

Ssex

varchar (20)

学生性别

Sbirthday

Datetime

学生出生年月

 Class

varchar (20)

学生所在班级

 

      表(二)Course(课程表)

属性名

数据类型

可否为空

含 义

Cno

varchar (20)

课程号(主码)

Cname

varchar (20)

课程名称

Tno

varchar (20)

教工编号(外码)

      表(三)Score(成绩表)

属性名

数据类型

可否为空

含 义

Sno

varchar (20)

学号(外码)

Cno

varchar (20)

课程号(外码)

Degree

Decimal(4,1)

 

成绩

主码:Sno+ Cno

      表(四)Teacher(教师表)

属性名

数据类型

可否为空

含 义

Tno

varchar (20)

教工编号(主码)

Tname

varchar (20)

教工姓名

Tsex

varchar (20)

教工性别

Tbirthday

datetime

教工出生年月

Prof

varchar (20)

职称

Depart

varchar (20)

教工所在部门

表1-2数据库中的数据

    表(一)Student

Sno

Sname

Ssex

Sbirthday

class

108

曾华

1977-09-01

95033

105

匡明

1975-10-02

95031

107

王丽

1976-01-23

95033

101

李军

1976-02-20

95033

109

王芳

1975-02-10

95031

103

陆君

1974-06-03

95031

 

    表(二)Course

Cno

Cname

Tno

3-105

计算机导论

825

3-245

操作系统

804

6-166

数字电路

856

9-888

高等数学

831

    表(三)Score

Sno

Cno

Degree

103

3-245

86

105

3-245

75

109

3-245

68

103

3-105

92

105

3-105

88

109

3-105

76

101

3-105

64

107

3-105

91

108

3-105

78

101

6-166

85

107

6-166

79

108

6-166

81

    表(四)Teacher

Tno

Tname

Tsex

Tbirthday

Prof

Depart

804

李诚

1958-12-02

副教授

计算机系

856

张旭

1969-03-12

讲师

电子工程系

825

王萍

1972-05-05

助教

计算机系

831

刘冰

1977-08-14

助教

电子工程系

 

数据库练习题目

1、 查询Student表中的所有记录的Sname、Ssex和Class列。

 

2、 查询教师所有的单位即不重复的Depart列。

 

3、 查询Student表的所有记录。

4、 查询Score表中成绩在60到80之间的所有记录。

 

5、 查询Score表中成绩为85,86或88的记录。

 

6、 查询Student表中“95031”班或性别为“女”的同学记录。

 以Class降序查询Student表的所有记录。

 以Cno升序、Degree降序查询Score表的所有记录。

 查询“95031”班的学生人数。

10、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)

 查询每门课的平均成绩。

12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。

查询分数大于70,小于90的Sno列。

查询所有学生的Sname、Cno和Degree列。

 

15、查询所有学生的Sno、Cname和Degree列。

16、查询所有学生的Sname、Cname和Degree列。

 

 

17、 查询“95033”班学生的平均分。

 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。

20、查询score中选学多门课程的同学中分数为非最高分成绩的记录。

 

21 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。

22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。

23、查询“张旭“教师任课的学生成绩(姓名)。

  1、从成绩表 查询学生成绩 

 

  2、条件是这个课程是 “张旭“教师任课

思路:A)从老师表 中查名字叫 张旭 编号

B)根据老师编号 在课程表中找到对应的 课程号

 

 

查询考计算机导论的学生成绩

 

查询李诚老师教的课程名称

 

教高等数学的老师是哪个系的

24、查询选修某课程的同学人数多于5人的教师姓名。

25、查询95033班和95031班全体学生的记录。

26、  查询存在有85分以上成绩的课程Cno.

27、查询出“计算机系“教师所教课程的成绩表。

29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学    的Cno、Sno和Degree,并按Degree从高到低次序排序。

30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的  Cno、Sno和Degree.

31、 查询所有教师和同学的name、sex和birthday.

32、查询所有“女”教师和“女”同学的name、sex和birthday.

33、 查询成绩比该课程平均成绩低的同学的成绩表。

34、 查询所有任课教师的Tname和Depart.

35、查询所有未讲课的教师的Tname和Depart. 

36、查询至少有2名男生的班号。

37、查询Student表中不姓“王”的同学记录。

38、查询Student表中每个学生的姓名和年龄。

39、查询Student表中最大和最小的Sbirthday日期值。

40、以班号和年龄从大到小的顺序查询Student表中的全部记录。

41、查询“男”教师及其所上的课程。

42、查询最高分同学的Sno、Cno和Degree列。

43、查询和“李军”同性别的所有同学的Sname.

44、查询和“李军”同性别并同班的同学Sname.

45、查询所有选修“计算机导论”课程的“男”同学的成绩表。

 

 

 

建表答案:

-- 学生表
CREATE TABLE student(
    sno VARCHAR(20) NOT NULL PRIMARY KEY,
    sname VARCHAR(20) NOT NULL,
    ssex VARCHAR(20) NOT NULL,
    sbirthday DATE,
    class VARCHAR(20)
)
-- 教师表
CREATE TABLE teacher(
    tno VARCHAR(20) NOT NULL PRIMARY KEY,
    tname VARCHAR(20) NOT NULL,
    tsex VARCHAR(20) NOT NULL,
    tbirthday DATE,
    prof VARCHAR(20),
    depart VARCHAR(20) NOT NULL

)

-- 课程表
CREATE TABLE course(
    cno VARCHAR(20)  PRIMARY KEY NOT NULL ,
    cname VARCHAR(20) NOT NULL,
    tno VARCHAR(20) NOT NULL ,
    CONSTRAINT course_teacher_fk FOREIGN KEY(tno)
    REFERENCES teacher(tno)
    
)
-- 成绩表
CREATE TABLE score(
    sno VARCHAR(20) NOT NULL,
    CONSTRAINT score_student_fk FOREIGN KEY(sno)
    REFERENCES student(sno),
    cno VARCHAR(20) NOT NULL ,
    CONSTRAINT score_course_fk FOREIGN KEY(cno)
    REFERENCES course(cno),
    degree DECIMAL(4,1) 
    
)
-- student
INSERT INTO student VALUES(108,曾华,,19770901,95033);
INSERT INTO student VALUES(105,匡明,,19751002,95031);
INSERT INTO student VALUES(107,王丽,,19760123,95033);
INSERT INTO student VALUES(101,李军,,19760220,95033);
INSERT INTO student VALUES(109,王芳,,19750210,95031);
INSERT INTO student VALUES(103,陆君,,19740603,95031);
SELECT * FROM student;
-- teacher 
INSERT INTO teacher VALUES(804,李诚,,19581202,副教授,计算机系);
INSERT INTO teacher VALUES(856,张旭,,19690312,讲师,电子工程系);
INSERT INTO teacher VALUES(825,王萍,,19720505,助教,计算机系);
INSERT INTO teacher VALUES(831,刘冰,,19770814,助教,电子工程系);
SELECT * FROM teacher;
-- course
INSERT INTO course VALUES(3-105,计算机导论,825);
INSERT INTO course VALUES(3-245,操作系统,804);
INSERT INTO course VALUES(6-166,数字电路,856);
INSERT INTO course VALUES(9-888,高等数学,831);
DELETE FROM student;
DELETE FROM teacher;
DELETE FROM course;
DELETE FROM score;
SELECT * FROM course;
-- score
INSERT INTO score VALUES(103,3-245,86);
INSERT INTO score VALUES(105,3-245,75);
INSERT INTO score VALUES(109,3-245,68);
INSERT INTO score VALUES(103,3-105,92);
INSERT INTO score VALUES(105,3-105,88);
INSERT INTO score VALUES(109,3-105,76);
INSERT INTO score VALUES(101,3-105,64);
INSERT INTO score VALUES(107,3-105,91);
INSERT INTO score VALUES(108,3-105,78);
INSERT INTO score VALUES(101,6-166,85);
INSERT INTO score VALUES(107,6-166,79);
INSERT INTO score VALUES(108,6-166,81);
SELECT * FROM score;

题目练习答案:

-- 1
SELECT sname,ssex,class FROM student ;
-- 2
SELECT DISTINCT depart FROM teacher;
-- 3
SELECT * FROM student;
-- 4
SELECT * FROM score WHERE degree BETWEEN 60 AND 80;
-- 5
SELECT * FROM score WHERE degree IN( 85,86,88);
-- 6
SELECT * FROM student WHERE class=95031 OR ssex=;
-- 7
SELECT * FROM student ORDER BY class DESC ;
-- 8
SELECT * FROM score ORDER BY cno ASC,degree DESC;
-- 9
SELECT COUNT(*)AS 学生人数 FROM student WHERE class=95031;
-- 10
SELECT sno AS 学生学号 ,cno AS 课程号 FROM score WHERE degree =(SELECT MAX(degree) FROM score);
-- 11
SELECT cno,AVG(degree) AS 平均成绩 FROM score   GROUP BY cno;
-- 12
SELECT cno,AVG(degree) AS 平均分数 FROM score WHERE cno LIKE 3% GROUP BY cno HAVING COUNT(cno)>=5  ;
-- 13
SELECT sno FROM score WHERE degree BETWEEN 70 AND 90;
-- 14
SELECT sname,cno,degree FROM score JOIN student ON 
    student.`sno`=score.`sno`;
-- 15
SELECT sno,cname,degree FROM score JOIN course ON
    score.`cno`= course.`cno`;
-- 16
SELECT sname,cname,degree FROM score JOIN student ON
    student.`sno`=score.`sno`
    JOIN course ON 
    score.`cno`= course.`cno`;
-- 17
SELECT AVG(degree) AS 平均分 FROM score WHERE sno IN(SELECT sno FROM student WHERE class=95033);
-- 18
SELECT * FROM student JOIN score ON student.`sno`= score.`sno`
    WHERE cno =3-105 
    AND degree>(SELECT degree FROM score WHERE sno =109 AND cno=3-105);
-- 20
-- 第一种
SELECT * FROM score a
    WHERE sno IN(SELECT sno FROM score GROUP BY sno HAVING COUNT(sno)>1 )
     AND degree not in (SELECT MAX(degree) FROM score b 
                where a.`cno`=b.`cno` );
-- 第二种
SELECT * FROM score a
    WHERE sno IN(SELECT sno FROM score GROUP BY sno HAVING COUNT(sno)>1 )
    and degree NOT IN (SELECT MAX(degree) FROM score 
            where sno in(SELECT sno FROM score 
                GROUP BY sno HAVING COUNT(sno)>1)
    
    
-- 21

SELECT * FROM student JOIN score ON student.`sno`= score.`sno`
    WHERE cno =3-105 
    AND degree>(SELECT degree FROM score WHERE sno =109AND cno =3-105);
-- 22
SELECT sno,sname,sbirthday FROM student WHERE 
    LEFT(sbirthday,4) = (SELECT LEFT(sbirthday,4) FROM student WHERE sno =108);
-- 23
SELECT sname AS 姓名,degree AS 学生成绩 FROM student 
    JOIN score ON student.`sno`= score.`sno`
    WHERE cno=( SELECT cno FROM course  
            JOIN teacher ON course.`tno`= teacher.`tno`
            WHERE tname =张旭)
-- 23.1

SELECT sname AS 姓名,degree AS 学生成绩 FROM student 
    JOIN score ON student.`sno`= score.`sno`
    WHERE cno =(SELECT cno FROM course WHERE cname =计算机导论);
-- 23.2
SELECT cname AS 课程名称 FROM course
    JOIN teacher ON course.`tno`=teacher.`tno`
    WHERE  tname =李诚;
-- 23.3

SELECT depart FROM teacher 
    WHERE tno=(SELECT tno FROM course WHERE cname =高等数学);
-- 24
SELECT tname AS 教师姓名 FROM teacher JOIN course ON teacher.`tno`=course.`tno`
    WHERE cno =(SELECT cno FROM score GROUP BY cno
            HAVING COUNT(cno)>5);
-- 25
SELECT score.sno,sname,ssex,sbirthday,class,score.cno,degree,cname,tname FROM student 
    JOIN score ON student.`sno`=score.`sno`
    JOIN course ON score.`cno` =course.`cno`
    JOIN teacher ON course.`tno`=teacher.`tno`
    WHERE class =95033OR class =95031;
      
-- 26
SELECT DISTINCT (score.`cno`) FROM course JOIN score ON score.`cno` =course.`cno`
    WHERE  degree >85
-- 27
SELECT score.* FROM score 
    JOIN course ON score.`cno` =course.`cno`
    JOIN teacher ON course.`tno`=teacher.`tno`
    WHERE  depart = 计算机系
    
-- 28
-- 29

SELECT cno,sno,degree FROM score 
    WHERE cno =3-105 AND 
    degree>ANY(SELECT degree FROM score WHERE cno=3-245)
    ORDER BY degree DESC;
-- 30
SELECT cno,sno,degree FROM score 
    WHERE cno =3-105 AND 
    degree>ALL(SELECT degree FROM score WHERE cno=3-245)
-- 31
SELECT sname AS NAME,ssex AS sex,sbirthday AS birthday FROM student 
    UNION    
SELECT tname,tsex,tbirthday FROM teacher    
-- 32
SELECT sname AS NAME,ssex AS sex,sbirthday AS birthday FROM student
    WHERE ssex=
UNION    
SELECT tname,tsex,tbirthday FROM teacher
    WHERE tsex=
-- 33
-- 方法一
SELECT * FROM score 
    WHERE cno=3-105
        AND degree <(SELECT AVG(degree)FROM score GROUP BY cno HAVING cno=3-105)
       OR cno=3-245
        AND degree <(SELECT AVG(degree)FROM score GROUP BY cno HAVING cno=3-245)
       OR cno=6-166
        AND degree <(SELECT AVG(degree)FROM score GROUP BY cno HAVING cno=6-166)


-- 方法二
select * from score a
    where degree<(SELECT AVG(degree)FROM score b
        where a.cno=b.cno);
        
    

-- 34
SELECT tname,depart FROM teacher 
    WHERE tno IN(SELECT DISTINCT (course.tno) FROM score 
            JOIN course ON score.`cno`=course.`cno` );

-- 35
SELECT tname,depart FROM teacher 
    WHERE tno  NOT IN(SELECT DISTINCT (course.tno) FROM score 
             JOIN course ON score.`cno`=course.`cno` );

-- 36
SELECT class AS 班号 FROM student 
    WHERE ssex =GROUP BY class HAVING COUNT(*)>=2 

-- 37
SELECT * FROM student WHERE sname  NOT LIKE 王%

-- 38
SELECT sname AS 姓名,YEAR(NOW())-YEAR(sbirthday) AS 年龄 FROM student
-- 39
SELECT MAX(sbirthday),MIN(sbirthday) FROM student;

-- 40
SELECT * FROM student ORDER BY class DESC,sbirthday ;
-- 41
SELECT tname AS 教师名,cname AS 课程名 FROM teacher 
    JOIN course ON teacher.`tno`=course.`tno`
    WHERE tsex =
-- 42
SELECT sno,cno,degree FROM score 
    WHERE degree=(SELECT MAX(degree) FROM score )
-- 43
SELECT sname FROM student 
    WHERE ssex= (SELECT ssex FROM student WHERE sname =李军)
        AND sname NOT IN(李军)
-- 44
SELECT sname FROM student 
    WHERE ssex= (SELECT ssex FROM student WHERE sname =李军)
        AND sname NOT IN(李军)
        AND class=(SELECT class FROM student WHERE sname =李军)
        
-- 45
SELECT *FROM score     
    WHERE cno =(SELECT cno FROM course WHERE cname =计算机导论)
        AND sno IN(SELECT sno FROM student WHERE ssex =)
            

 

数据库练习题

标签:des   数字   gre   esc   答案   子查询   date   nbsp   ike   

原文地址:https://www.cnblogs.com/lzw123-/p/9304201.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!