标签:
设计一个学生成绩数据库,该库包含学生,老师,课程和成绩等信息并完成后面的练习。
学生:学号(SNO)、姓名(SNAME)、性别(SSEX)、生日(SBIRTHDAY )、所属班级(SCLASS )
课程:课程编号(CNO)、课程名(CNAME)、授课老师(TNO)
成绩:学号(SNO)、课程编号(CNO)、得分(DEGREE)
老师:教师编号(TNO)、教师姓名(TNAME)、性别(TSSEX)、生日(TBIRTHDAY)、职称(TITLE)、单位科室(DEPART)
要求:
CREATE DATABASE grades;
CREATE TABLE IF NOT EXISTS student(
sno TINYINT UNSIGNED KEY NOT NULL,
sname VARCHAR(20) NOT NULL,
ssex ENUM(‘男‘,‘女‘) DEFAULT ‘男‘,
sbirthday DATE,
sclass TINYINT NOT NULL
);
INSERT student VALUES(1,‘ONE‘,DEFAULT,120101,1),(2,‘two‘,‘女‘,120202,2),(3,‘three‘,‘女‘,120303,3),(4,‘four‘,‘女‘,120404,1),(5,‘five‘,DEFAULT,120505,1),(6,‘six‘,DEFAULT,120606,1),(7,‘seeven‘,DEFAULT,120707,2),(8,‘eight‘,DEFAULT,120808,2),(9,‘nine‘,DEFAULT,120909,1),(10,‘ten‘,DEFAULT,121010,1);
CREATE TABLE IF NOT EXISTS class(
cno TINYINT UNSIGNED NOT NULL UNIQUE,
cname VARCHAR(20) NOT NULL,
tno TINYINT NOT NULL,
FOREIGN KEY(tno) REFERENCES teacher(tno)
);
INSERT class VALUES(1,‘chinese‘,1),(2,‘math‘,2),(3,‘englidh‘,3),(4,‘physics‘,4),(5,‘chemical‘,4),(6,‘biology‘,4),(7,‘music‘,5),(8,‘gym‘,6),(9,‘nature‘,7),(10,‘politics‘,8);
CREATE TABLE IF NOT EXISTS grades(
sno TINYINT UNSIGNED NOT NULL,
cno TINYINT UNSIGNED NOT NULL,
degree TINYINT
);
INSERT grades VALUES(1,1,40),(1,2,50),(1,3,60),(1,4,80),(1,5,80),(2,6,96),(2,7,96),(2,9,96),(3,8,96),(3,10,96);
CREATE TABLE IF NOT EXISTS teacher(
tno TINYINT UNSIGNED KEY NOT NULL,
tname VARCHAR(20) NOT NULL,
tsex ENUM(‘男‘,‘女‘) DEFAULT ‘男‘,
tbirthday DATE,
title VARCHAR(10),
depart VARCHAR(10)
);
INSERT teacher VALUES(1,‘wang‘,DEFAULT,120101,‘teacher‘,‘teach‘),(2,‘li‘,DEFAULT,120202,‘teacher‘,‘teach‘),(3,‘zhang‘,‘女‘,120303,‘teacher‘,‘teach‘),(4,‘peng‘,DEFAULT,120505,‘teacher‘,‘polic‘),(5,‘mozart‘,DEFAULT,120606,‘teacher‘,‘polic‘),(6,‘strog‘,DEFAULT,120707,‘teacher‘,‘teach‘),(7,‘leave‘,DEFAULT,120808,‘teacher‘,‘teach‘),(8,‘xidada‘,DEFAULT,120909,‘teacher‘,‘manager‘);
1、 查询Student表中的所有记录的Sname、Ssex和Class列。
SELECT sname,ssex,sclass FROM student;
2、 查询教师所有的单位即不重复的Depart列。
SELECT depart,group_concat(tname) FROM teacher GROUP BY depart;
3、 查询Student表的所有记录。
SELECT * FROM student;
4、 查询grades表中成绩在60到80之间的所有记录。
SELECT * FROM grades WHERE degree BETWEEN 60 AND 80;
5、 查询grades表中成绩为85,86或88的记录。
SELECT * FROM grades WHERE degree=85 OR degree=80 OR degree=86;
6、 查询Student表中“95031”班或性别为“女”的同学记录。
SELECT * FROM student WHERE sclass=85 OR ssex=‘女‘;
7、 以Class降序查询Student表的所有记录。
SELECT * FROM student ORDER BY sno DESC;
8、 以Cno升序、Degree降序查询grades表的所有记录。
SELECT * FROM grades ORDER BY cno ASC,degree DESC;
9、 查询“95031”班的学生人数。
SELECT count(*) FROM class WHERE cno=95031;
10、查询grades表中的最高分的学生学号和课程号。
SELECT max(degree),sno,cno FROM grades;
11、查询‘3-105’号课程的平均分。
SELECT AVG(degree) FROM grades WHERE cno BETWEEN 3 AND 105;
12、查询grades表中至少有5名学生选修的并以3开头的课程的平均分数。
SELECT AVG(degree) FROM grades WHERE sno>4 AND cno REGEXP ‘^3‘;
13、查询最低分大于70,最高分小于90的Sno列。
SELECT sno FROM grades WHERE degree>70 AND degree<90;
14、查询所有学生的Sname、Cno和Degree列。
SELECT s.sname,c.cno,g.degree FROM student AS s JOIN grades AS g ON g.sno=s.sno JOIN class AS c ON g.cno=c.cno;
15、查询所有学生的Sno、Cname和Degree列。
SELECT s.sno,c.cname,g.degree FROM student AS s JOIN grades AS g ON g.sno=s.sno JOIN class AS c ON g.cno=c.cno;
16、查询所有学生的Sname、Cname和Degree列。
SELECT s.sname,c.cname,g.degree FROM student AS s JOIN grades AS g ON g.sno=s.sno JOIN class AS c ON g.cno=c.cno;
17、查询“95033”班所选课程的平均分。
SELECT AVG(degree) FROM grades WHERE cno=95031;
18、假设使用如下命令建立了一个grade表:
create table grade(
low int,
upp int,
rank char(1)
);
现查询所有同学的Sno、Cno和rank列。
SELECT s.sno,c.cno,g.rank FROM student AS s JOIN grade AS g ON g.sno=s.sno JOIN class AS c ON g.cno=c.cno;
19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
select * from student as s join grades as g on s.sno=g.sno where g.degree>(select g.degree from grades where s.sno=109) and g.cno=3-105;
20、查询grades中选学一门以上课程的同学中分数为非最高分成绩的记录。
select * from grades where
21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
select * from grades as g join student as s on s.sno=g.sno where g.degree>(select g.degree from grades where s.sno=109) and g.cno=3-105;
22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
select sno,sname,sbirthday from student where sbirthday=(select sbirthday from student where sno=108);
23、查询“张旭“教师任课的学生成绩。
select * from grades as g join class as c on c.cno=g.cno join teacher as t on t.tno=c.tno where t.tname=‘张旭‘;
24、查询选修某课程的同学人数多于5人的教师姓名。
25、查询95033班和95031班全体学生的记录。
select s.* from student as s join class as c on s.sclass=c.cname where c.cname=95033 or c.cname=95031;
26、查询存在有85分以上成绩的课程Cno.
select cno from grades where degree>85;
27、查询出“计算机系“教师所教课程的成绩表。
select * from grades as g join class as c on c.cno=g.cno join teacher as t on t.tno=c.tno where t.depart=‘计算机系‘;
28、查询“计算机系”与“电子工程系“不同职称的教师的Tname。
select tname from teacher where depart=‘计算机系‘ or depart=‘电子工程系‘ group by title;
29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
select cno,sno,degree from grades where sno=3-105 and degree>(select degree from grades where sno=3-245) order by degree asc;
30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
select cno,sno,degree from grades where sno=3-105 and degree>(select degree from grades where sno=3-245);
31、查询所有教师和同学的name、sex和birthday.
select sname,ssex,sbirthday from student union select tname,tsex,tbirthday from teacher;
32、查询所有“女”教师和“女”同学的name、sex和birthday.
select sname,sbirthday from student where ssex=‘女‘ union select tname,tbirthday from teacher where tsex=‘女‘;
33、查询成绩比该课程平均成绩低的同学的成绩表。
select group_concat(degree),avg(degree) from grades group by degree having degree<avg(degree);
34、查询所有任课教师的Tname和Depart.
SELECT tname,depart FROM teacher;
35 查询所有未讲课的教师的Tname和Depart.
SELECT t.tname,t.depart FROM teacher as t join class as c on t.tno=c.tno where c.tno=NULL;
36、查询至少有2名男生的班号。
SELECT c.cno FORM class AS c JOIN grades AS g ON g.cno=c.cno join student as s on s.sno=g.sno where s.ssex=‘男‘;
37、查询Student表中不姓“王”的同学记录。
SELECT sname FROM student WHERE sname NOT LIKE ‘王%‘;
38、查询Student表中每个学生的姓名和年龄。
SELECT sname,sbirthday FROM student;
39、查询Student表中最大和最小的Sbirthday日期值。
SELECT MAX(sbirthday) AS ODLDEST,MIN(sbirthday) AS YOUNGEST FROM student;
40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
SELECT * FROM student ORDER BY sclass DESC,sbirthday DESC;
41、查询“男”教师及其所上的课程。
SELECT c.cname,t.tname,t.tsex FROM class AS c JOIN teacher AS t ON c.tno=t.tno WHERE t.tsex=‘男‘;
42、查询最高分同学的Sno、Cno和Degree列。
SELECT MAX(g.degree),s.sno,c.cno,g.degree FROM student AS s JOIN grades AS g ON g.sno=s.sno JOIN class AS c ON g.cno=c.cno;
43、查询和“李军”同性别的所有同学的Sname.
SELECT sname FROM student WHERE ssex=(SELECT ssex FROM student WHERE sname=‘李军‘);
44、查询和“李军”同性别并同班的同学Sname.
SELECT sname FROM student WHERE ssex=(SELECT ssex FROM student WHERE sname=‘李军‘) AND sclass=(SELECT sclass FROM student WHERE sname=‘李军‘);
45、查询所有选修“计算机导论”课程的“男”同学的成绩表
select * from grades as g join class as c on c.cno=g.cno join student as s on s.sno=g.sno where s.ssex=‘男‘ and c.cname=‘计算机导论‘;
标签:
原文地址:http://www.cnblogs.com/pengsixiong/p/4945088.html