7月27日 MySql数据库周作业

表结构脚本:

CREATE TABLE Student 
(
no int(50) not null PRIMARY key, -- 学号
name VARCHAR(50) not null, -- 学生姓名
sex VARCHAR(50) not null, -- 性别
birthday VARCHAR(50) not null, -- 生日
class VARCHAR(50) not null -- 班级
)
CREATE TABLE Teacher 
(
no int(50) not null PRIMARY key, -- 教师编号
name VARCHAR(50) not null, -- 教师姓名
 sex VARCHAR(50) not null, -- 教师性别
 birthday datetime, -- 教师生日
 prof VARCHAR(50) not null, -- 职位
 depart VARCHAR(50) not null -- 系别
)
CREATE TABLE Course 
(
cno VARCHAR(50) not null PRIMARY key, -- 课程编号
cname VARCHAR(50) not null, -- 课程
 tno int(50) not null -- 教师编号
)
CREATE TABLE Score 
(
no int(50) not null , -- 学号
cno VARCHAR(50) not null, -- 课程编号
degree int(50) not null -- 成绩
)
DROP TABLE Score;
INSERT INTO course VALUES ('3-101','数据库',1);
INSERT INTO course VALUES ('5-102','数学',3);
INSERT INTO course VALUES ('3-103','信息系统',4);
INSERT INTO course VALUES ('3-104','操作系统',6);
INSERT INTO course VALUES ('3-105','数据结构',4);
INSERT INTO course VALUES ('3-106','数据处理',5);
INSERT INTO course VALUES ('4-107','pascal语言',5);
INSERT INTO course VALUES ('4-108','c++',7);
INSERT INTO course VALUES ('4-109','java',8);
INSERT INTO course VALUES ('3-245','数据挖掘',10);
INSERT INTO course VALUES ('3-111','软件工程',11);

INSERT INTO score VALUES(5001,'3-105',69);
INSERT INTO score VALUES(5001,'5-102',55);
INSERT INTO score VALUES(5003,'4-108',85);
INSERT INTO score VALUES(5004,'3-105',77);
INSERT INTO score VALUES(5005,'3-245',100);
INSERT INTO score VALUES(5006,'3-105',53);
INSERT INTO score VALUES(5003,'4-109',45);
INSERT INTO score VALUES(5008,'3-105',98);
INSERT INTO score VALUES(5004,'4-109',68);
INSERT INTO score VALUES(5010,'3-105',88);
INSERT INTO score VALUES(5003,'3-105',98);
INSERT INTO score VALUES(5005,'4-109',68);
INSERT INTO score VALUES(5002,'3-105',88);
INSERT INTO score VALUES(107,'3-105',98);
INSERT INTO score VALUES(108,'4-109',68);
INSERT INTO score VALUES(109,'3-105',88);
INSERT INTO score VALUES(109,'4-109',80);
INSERT INTO score VALUES(107,'3-111',88);
INSERT INTO score VALUES(5003,'3-111',80);


INSERT INTO Student VALUES(5001,'李勇','男','1987-7-22','95001');
INSERT INTO Student VALUES(5002,'刘晨','女','1987-11-15','95002');
INSERT INTO Student VALUES(5003,'王敏','女','1998-10-5','95001');
INSERT INTO Student VALUES(5004,'李好尚','男','1987-9-25','95003');
INSERT INTO Student VALUES(5005,'李军','男','1987-7-17','95004');
INSERT INTO Student VALUES(5006,'范新位','女','1987-6-18','95005');
INSERT INTO Student VALUES(5007,'张霞东','女','1987-8-29','95006');
INSERT INTO Student VALUES(5008,'赵薇','男','1987-6-15','95007');
INSERT INTO Student VALUES(5009,'钱民将','女','1987-6-23','95008');
INSERT INTO Student VALUES(5010,'孙俪','女','1987-9-24','95002');
INSERT INTO Student VALUES(108,'赵里','男','1987-6-15','95007');
INSERT INTO Student VALUES(109,'丘处机','男','1987-6-23','95008');
INSERT INTO Student VALUES(107,'杨康','男','1987-9-24','95001');


INSERT INTO Teacher VALUES(1,'李卫','男','1957-11-5','教授','电子工程系');
INSERT INTO Teacher VALUES(2,'刘备','男','1967-10-9','副教授','math');
INSERT INTO Teacher VALUES(3,'关羽','男','1977-9-20','讲师','sc');
INSERT INTO Teacher VALUES(4,'李修','男','1957-6-25','教授','elec');
INSERT INTO Teacher VALUES(5,'诸葛亮','男','1977-6-15','教授','计算机系');
INSERT INTO Teacher VALUES(6,'殷素素','女','1967-1-5','副教授','sc');
INSERT INTO Teacher VALUES(7,'周芷若','女','1947-2-3','教授','sc');
INSERT INTO Teacher VALUES(8,'赵云','男','1980-6-13','副教授','计算机系');
INSERT INTO Teacher VALUES(9,'张敏','女','1985-5-5','助教','sc');
INSERT INTO Teacher VALUES(10,'黄蓉','女','1967-3-22','副教授','sc');
INSERT INTO Teacher VALUES(11,'张三','男','1967-3-22','副教授','sc');



参考答案:

-- 3.    以class降序输出student的所有记录(student表全部属性)
select * from student ORDER BY class DESC
-- 4.    列出教师所在的单位depart(不重复)。
select DISTINCT depart from teacher 
-- 5.列出student表中所有记录的name、sex和class列
select name,sex,class from student
-- 6.    输出student中不姓王的同学的姓名。
select name from student where name not LIKE '王%'
-- 7.    输出成绩为85或86或88或在60-80之间的记录(no,cno,degree)
select no,cno,degree from score where degree in(85,86,88) or degree between 60 and 80
-- 8.    输出班级为95001或性别为‘女’ 的同学(student表全部属性)
select * from student where class='95001' or sex='女'
-- 9.    输出男生人数及这些男生分布在多少个班级中
SELECT COUNT(sex),COUNT(DISTINCT class) from student where sex='男'
-- 10.    列出存在有85分以上成绩的课程编号。
SELECT DISTINCT cno from score where degree>85
-- 11.输出95001班级的学生人数
select COUNT(*) from Student where class='95001'
-- 12.输出‘3-105’号课程的平均分
select AVG(DEGREE) from Score where cno='3-105'
-- 13.输出student中最大和最小的birthday日期值
select MAX(birthday),MIN(birthday) from student
-- 14.显示95001和95004班全体学生的全部个人信息(不包括选课)。(student表全部属性)
select * from Student where class='95001' or class='95004'
-- 15.输出至少有5个同学选修的并以3开头的课程的课程号,课程平均分,课程最高分,课程最低分。
select cno,AVG(degree),MAX(degree),MIN(degree) 
from Score 
where cno like '3%'
group by cno 
having COUNT(no)>5
-- 16.输出所选修课程中最低分大于70分且最高分小于90分的学生学号及学生姓名
select student.no,student.name from Score inner join Student on student.no=score.no 
group by student.no,name having (MIN(degree)>70 and MAX(degree)<90)
-- 17.显示所教课程选修人数多于5人的教师姓名
select name from Score inner join course on score.cno=course.cno inner join Teacher on Teacher.no=course.tno
 group by teacher.no,name having COUNT(*)>5
-- 18.输出’95001’班级所选课程的课程号和平均分
select cno,AVG(degree) from Student inner join Score on student.no=score.no
 where student.class='95001' group by cno
-- 19.输出至少有两名男同学的班级编号。
select class from Student  where sex='男'
 group by class having COUNT(distinct no)>1
-- 20.列出与108号同学同年出生的所有学生的学号、姓名和生日
select no,name,birthday from Student where year(birthday)=(select YEAR(birthday) from Student where no =108)
-- 21.列出存在有85分以上成绩的课程名称
select cname from course inner join Score on course.cno=score.cno group by cname having MAX(degree)>85
-- 22.列出“计算机系”教师所教课程的成绩表(课程编号,课程名,学生名,成绩)。
select score.cno,cname,student.name,DEGREE from Student
 inner join Score on student.no=score.no 
inner join course on score.cno=course.cno 
inner join Teacher on course.tno=Teacher.no 
where teacher.depart='计算机系'
-- 23.列出所有可能的“计算机系”与“电子工程系”不同职称的教师配对信息,要求输出每个老师的姓名(name)和(职称)
select ex1.name,ex1.prof,ex2.name,ex2.prof from Teacher ex1,Teacher ex2 
where ex1.depart='计算机系' and ex2.depart='电子工程系' and ex1.prof!=ex2.prof
-- 24.显示‘张三’教师任课的学生姓名,课程名,成绩
select student.name,cname,DEGREE from Student 
inner join Score on student.no=score.no 
inner join course on score.cno=course.cno 
inner join Teacher on course.tno=teacher.no 
where teacher.name='张三'
-- 25.列出所讲课已被选修的教师的姓名和系别
select distinct teacher.name,depart from Score 
inner join course on score.cno=course.cno 
inner join Teacher on course.tno=Teacher.no
-- 26.输出所有学生的name、no和degree。(degree为空的不输出和为空的输出两种情况)(内连接和左连接)。
select student.name,student.no,DEGREE from Student inner join Score on student.no=score.no
select student.name,student.no,DEGREE from Student left join Score on student.no=score.no
-- 27.列出所有任课教师的name和depart。(从课程选修和任课两个角度考虑)
(课程选修)
select distinct teacher.name,depart
from score
left join course on score.cno=course.cno
left join teacher on course.tno=teacher.no

(任课)
select  DISTINCT teacher.name,depart from teacher inner join course on Teacher.no=course.tno
-- 28.输出男教师所上课程名称。
select cname from Teacher inner join course on Teacher.no=course.tno where teacher.sex='男'

-- 29.出与“李军”同性别的所有同学的name。
select name from Student where sex=(select sex from Student where name='李军')

-- 30.输出选修“数据结构”课程的男同学的成绩。
select DEGREE from Student
inner join Score on student.no=score.no
INNER JOIN course ON course.cno=score.cno
where sex='男'
AND cname='数据结构';
-- 31.列出选修编号为‘3-105’课程并且该门课程成绩比课程 ‘3-111’的最高分要高的cno,no和degree。
select cno,student.no,DEGREE from Student 
inner join Score on student.no=score.no 
where cno='3-105' and degree>(select MAX(degree) from Score where cno='3-111')
-- 32.输出score中成绩最高的学号和课程号
select no,cno from Score where degree=(select MAX(degree) from Score)
-- 33.输出选修3-105课程,其成绩高于109号同学在此课程所得成绩的所有同学的学号,姓名
select student.no,name from Student 
inner join Score on student.no=score.no 
where cno='3-105' and 
degree>(select degree from Student 
inner join Score on student.no=score.no 
where student.no=109 and cno='3-105')
-- 34.列出没有实际授课的教师的姓名和系别
select distinct name,depart 
from Teacher 
left join course on Teacher.no=course.tno 
left join Score on course.cno=score.cno 
where score.no is null
-- 35.列出选修了编号为‘3-105’课程且其成绩高于‘4-109’课程最高成绩的同学的 课程编号,学号和成绩
select cno,student.no,DEGREE from Student 
inner join Score on student.no=score.no 
where cno='3-105' and 
degree>(select MAX(degree) from Score where cno='4-109')

猜你喜欢

转载自blog.csdn.net/Percy0618/article/details/81284217
今日推荐