云想衣裳花想容,春风拂槛露华浓。
若非群玉山头见,会向瑶台月下逢。

现在有一教学管理系统,具体的关系模式如下:
Student (no, name, sex, birthday, class)
Teacher (no, name, sex, birthday, prof, depart)
Course (cno, cname, tno)
Score (no, cno, degree)
其中表中包含如下数据:
Course表:

Score表:

Student表:

Teacher表:

根据上面描述完成下面问题:
(注意:注意保存脚本,尤其是DDL和DML,以便进行数据还原)
DDL
1.写出上述表的建表语句。

命令:
create table Student (no int, name varchar(30), sex char(2), birthday datetime, class int);
create table Teacher (no int, name varchar(30), sex char(2), birthday datetime, prof varchar(20), depart varchar(30));
create table Course (cno varchar(10), cname varchar(30), tno int);
create table Score (no int, cno varchar(10), degree float);
DML
2.给出相应的INSERT语句来完成题中给出数据的插入。

命令:
insert into Student values(5001,'李勇','男','1987-07-22 00:00:00.000',95001);
insert into Student values(5002,'刘晨','女','1987-11-15 00:00:00.000',95002);
insert into Student values(5003,'王敏','女','1987-10-5 00:00:00.000',95001);
insert into Student values(5004,'李好尚','男','1987-9-25 00:00:00.000',95003);
insert into Student values(5005,'李军','男','1987-7-17 00:00:00.000',95004);
insert into Student values(5006,'范新位','女','1987-6-18 00:00:00.000',95005);
insert into Student values(5007,'张霞东','女','1987-8-29 00:00:00.000',95006);
insert into Student values(5008,'赵薇','男','1987-6-15 00:00:00.000',95007);
insert into Student values(5009,'钱民将','女','1987-6-23 00:00:00.000',95008);
insert into Student values(5010,'孙俪','女','1987-9-24 00:00:00.000',95002);
insert into Student values(108,'赵里','男','1987-6-15 00:00:00.000',95007);
insert into Student values(109,'丘处机','男','1987-6-23 00:00:00.000',95008);
insert into Student values(107,'杨康','男','1987-9-24 00:00:00.000',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','讲师','cs');
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','副教授','cs');
insert into Teacher values('7','周芷若','女','1947-2-23','教授','cs');
insert into Teacher values('8','赵云','男','1980-6-15','副教授','计算机系');
insert into Teacher values('9','张敏','女','1985-5-5','助教','cs');
insert into Teacher values('10','黄蓉','女','1967-3-22','副教授','cs');
insert into Teacher values('11','张三','男','1967-3-22','副教授','cs');

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);
单表查询
3.以class降序输出student的所有记录(student表全部属性)
命令:
select * from Student order by class desc;

4.列出教师所在的单位depart(不重复)。
命令:
select count(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.以cno升序、degree降序输出score的所有记录。(score表全部属性)
命令:
select * from Score order by cno asc, degree desc;

10.输出男生人数及这些男生分布在多少个班级中
命令:
select count(*), count(distinct class) from Student where sex = '男';

11.列出存在有85分以上成绩的课程编号。
命令:
select cno from Score where degree > 85 group by cno;

12.输出95001班级的学生人数
命令:
select count(*) from Student where class = 95001;

13.输出‘3-105’号课程的平均分
命令:
select avg(degree) from Score where cno = '3-105';

14.输出student中最大和最小的birthday日期值
命令:
select max(birthday), min(birthday) from Student;

15.显示95001和95004班全体学生的全部个人信息(不包括选课)。(student表全部属性)
命令:
select * from Student where class = 95001 or class = 95004;

聚合查询
16.输出至少有5个同学选修的并以3开头的课程的课程号,课程平均分,课程最高分,课程最低分。
命令:select cno, avg(degree), max(degree), min(degree) from Score where cno like '3%' group by cno having count(*) >= 5;

17.输出所选修课程中最低分大于70分且最高分小于90分的学生学号及学生姓名
命令:
select no,name from Student where no in(select no from Score group by no having min(degree) > 70 and max(degree) < 90);

18.显示所教课程选修人数多于5人的教师姓名
命令:
select name from Teacher where no in(select tno from
Course where cno in (select cno from score
group by cno having count(cno) > 5));

19.输出’95001’班级所选课程的课程号和平均分
命令:select cno, avg(degree) from Score where no in (select no from Student where class = 95001) group by cno;

20.输出至少有两名男同学的班级编号。
命令:select class from Student where sex = '男' group by class having count(*) >= 2;

多表查询
21.列出与108号同学同年出生的所有学生的学号、姓名和生日
命令:select a.no, a.name, a.birthday from Student a, Student b where b.no = 108 and year(b.birthday) = year(a.birthday)

22.列出存在有85分以上成绩的课程名称
命令:
select cname from Course where cno in (select cno from Score where degree > 85 group by cno);

select cname from Course, (select cno from Score where Score.degree > 85 group by cno) t where Course.cno = t.cno;

23.列出“计算机系”教师所教课程的成绩表(课程编号,课程名,学生名,成绩)。
命令:
select Score.cno, cname, Student.name, degree from Teacher, Course, Student, Score where Teacher.depart = '计算机系' and Teacher.no = Course.tno and Course.cno = Score.cno and Student.no = Score.no;

24.列出所有可能的“计算机系”与“电子工程系”不同职称的教师配对信息,要求输出每个老师的姓名(name)和(职称)
命令:select a.name, a.prof, b.name, b.prof from Teacher a, Teacher b where a.depart in ('计算机系', '电子工程') and b.depart in('计算机系', '电子工程') and a.prof != b.prof and a.depart != b.depart;

25.列出所有处于不同班级中,但具有相同生日的学生,要求输出每个学生的学号和姓名。(提示:使用datediff函数,具体用法可以参考:http://hcmfys.javaeye.com/blog/588844
命令:select a.no, a.name, b.no, b.name from Student a, Student b where a.class != b.class and datediff(day, a.birthday, b.birthday) = 0;

26.显示‘张三’教师任课的学生姓名,课程名,成绩
命令:select Student.name, cname, degree from Teacher, Student, Course, Score where Teacher.name = '张三' and Teacher.no = Course.tno and Course.cno = Score.cno and Student.no = Score.no;

27.列出所讲课已被选修的教师的姓名和系别
命令:select distinct name, depart from Teacher, Course, Score where Teacher.no = Course.tno and Course.cno = Score.cno;

28.输出所有学生的name、no和degree。(degree为空的不输出和为空的输出两种情况)。
命令:select name, Student.no, degree from Student, Score where Student.no = Score.no;
select name, Student.no, degree from Student left join Score on Student.no = Score.no;

29.列出所有任课教师的name和depart。(从课程选修和任课两个角度考虑)
命令:select distinct name, depart from Teacher, Course where Teacher.no = Course.tno;
select distinct name, depart from Teacher, Course, Score where Teacher.no = Course.tno and Score.cno = Course.cno;

30.输出男教师所上课程名称。
命令:
select cname from Teacher, Course where Teacher.sex = '男' and Course.tno = Teacher.no;

31.出与“李军”同性别的所有同学的name。
命令:
select a.name from Student a, Student b where a.sex = b.sex and b.name = '李军';

32.输出选修“数据结构”课程的男同学的成绩。
命令:
select degree from Score, Course, Student where Score.cno = Course.cno and Course.cname = '数据结构' and Student.no = Score.no and Student.sex = '男';

33.列出选修编号为‘3-105’课程且该门课程成绩比课程‘3-111’的最高分要高的cno,no和degree。
命令:select Score.cno, no, degree from Score, (select cno, max(degree) maxdegree from Score group by cno) t where Score.cno = '3-105' and t.cno = '3-111' and Score.degree > t.maxdegree;

子查询
34.输出score中成绩最高的学号和课程号
命令:select no, cno from Score where degree = (select max(degree) from Score);

35.输出选修3-105课程,其成绩高于109号同学在此课程所得成绩的所有同学的学号,姓名
命令:select a.no, Student.name from Score a, Score b, Student where a.cno = '3-105' and b.cno = '3-105'and b.no = 109 and a.degree > b.degree and a.no = Student.no;

36.列出成绩比该课程平均成绩低的同学的学号,成绩和该门课的平均成绩
命令:
select no, degree, avg_degree from Score, (select cno, avg(degree) avg_degree from Score group by cno) t where Score.degree < t.avg_degree and Score.cno = t.cno;

37.列出没有实际授课的教师的姓名和系别
命令:select distinct name, depart from Teacher where no not in (select tno from Course, Score where Course.cno = Score.cno);

38.列出选修了编号为‘3-105’课程且其成绩高于‘4-109’课程最高成绩的同学的 课程编号,学号和成绩
命令:
select cno, no, degree from Score where degree > (select max(degree) from Score where cno = '4-109') and cno = '3-105';

39.**列出符合下述条件的所有可能的同学配对(sno1,sname1,sno2,sname2,difference)。其中要求学号为sno1的sname1同学的所学课程的平均分大于学号为sno2的sname2同学的所学课程平均分,两个同学的课程平均分的差值difference为(sno1同学平均分-sno2同学平均分)
命令:
select a.no, a.name, b.no, b.name, c.avgdegree - d.avgdegree difference from Student a, Student b, (select no, avg(degree) avgdegree from Score group by no) c,(select no, avg(degree) avgdegree from Score group by no) d where a.no = c.no and b.no = d.no and c.avgdegree > d.avgdegree;

mysql新手进阶02的更多相关文章

  1. mysql新手进阶03

    当年忠贞为国酬,何曾怕断头? 如今天下红遍,江山靠谁守? 业未就,身躯倦,鬓已秋. 你我之辈,忍将夙愿,付与东流? 数据库结构如下: 仓库(仓库号, 城市, 面积) 订购单(职工号, 供应商号, 订购 ...

  2. mysql新手进阶01

    生活不止眼前的苟且,还有诗和远方. 请根据给出的数据库表结构来回答相应问题: DEPT (DEPTNO INT, DNAME VARCHAR(14),LOC VARCHAR(13)); EMP (EM ...

  3. springBoot进阶02

    SpringBoot进阶02 1. 日志的使用 1.1 基本使用 /** * 获取日志记录器 */ Logger logger = LoggerFactory.getLogger(this.getCl ...

  4. MYSQL(进阶篇)——一篇文章带你深入掌握MYSQL

    MYSQL(进阶篇)--一篇文章带你深入掌握MYSQL 我们在上篇文章中已经学习了MYSQL的基本语法和概念 在这篇文章中我们将讲解底层结构和一些新的语法帮助你更好的运用MYSQL 温馨提醒:该文章大 ...

  5. mysql 开发进阶篇系列 55 权限与安全(安全事项 )

    一. 操作系统层面安全 对于数据库来说,安全很重要,本章将从操作系统和数据库两个层面对mysql的安全问题进行了解. 1. 严格控制操作系统账号和权限 在数据库服务器上要严格控制操作系统的账号和权限, ...

  6. mysql 开发进阶篇系列 47 物理备份与恢复(xtrabackup 的完全备份恢复,恢复后重启失败总结)

    一. 完全备份恢复说明 xtrabackup二进制文件有一个xtrabackup --copy-back选项,它将备份复制到服务器的datadir目录下.下面是通过 --target-dir 指定完全 ...

  7. mysql 开发进阶篇系列 46 物理备份与恢复( xtrabackup的 选项说明,增加备份用户,完全备份案例)

    一. xtrabackup 选项说明 在操作xtrabackup备份与恢复之前,先看下该工具的选项,下面记录了xtrabackup二进制文件的部分命令行选项,后期把常用的选项在补上.点击查看xtrab ...

  8. mysql 开发进阶篇系列 42 逻辑备份与恢复(mysqldump 的完全恢复)

    一.概述 在作何数据库里,备份与恢复都是非常重要的.好的备份方法和备份策略将会使得数据库中的数据更加高效和安全.对于DBA来说,进行备份或恢复操作时要考虑的因素大概有如下: (1) 确定要备份的表的存 ...

  9. mysql 开发进阶篇系列 20 MySQL Server(innodb_lock_wait_timeout,innodb_support_xa,innodb _log_*)

    1. innodb_lock_wait_timeout mysql 可以自动监测行锁导致的死锁并进行相应的处理,但是对于表锁导致的死锁不能自动监测,所以该参数主要用于,出现类似情况的时候等待指定的时间 ...

随机推荐

  1. jQuery中append()、prepend()与after()、before()的区别

    转载 未曾见海  https://www.cnblogs.com/afei-qwerty/p/6682963.html 在jQuery中,添加元素有append(),prepend() 和 after ...

  2. MFC中用户自定义类响应自定义消息

    这篇技术文章不是讨论经典的MFC中的消息工作机理的,讨论消息工作原理.方式和路径的文章在网上和书本中随处可见.网上众多的讨论都是关于如何响应并进行用户自定义消息映射的:网上还有一些文章介绍如何在自定义 ...

  3. 【luogu P3393 逃离僵尸岛】 题解

    题目链接:https://www.luogu.org/problemnew/show/P3393 被占领的点可以先连在一个点上然后只需要对这一个点bfs一遍就可以求所有的危险点 #include &l ...

  4. Vue01 vue基础、mvvm、ES6z知识点、计算属性、生命周期

    Vue案例: <body> <div id="app"> <!--第一部分--> <fieldset> <legend> ...

  5. Number & Math

    Java Number & Math 类 一般地,当需要使用数字的时候,我们通常使用内置数据类型,如:byte.int.long.double 等. 实例 int a = 5000; floa ...

  6. 使用PIE.htc让万恶的IE内核浏览器IE6\7\8支持CSS3部分属性

    万恶的IE内核浏览器,这是多少前端程序员头疼的事情... 今天给大家介绍一下如何用 PIE.htc 来让IE浏览器支持CSS3的 border-radius.box-shadow.CSS3 Backg ...

  7. oracle入门(一)

    ### 一.体系结构 1. 数据库 : 只有一个数据库 2. 实例 : 后台运行的一个进程 3. 表空间: 逻辑存储单位 4. 数据文件: 物理存储单位 5. 用户:面向用户管理,由用户来管理表空间, ...

  8. Vue--- 一点车项目 6个小时实际看了10天(完结)

    一个项目 环境安装   使用了 cli 脚手架  Koa2  workpackage  其他小的不计 前端Vue组件搭建 数据的简单测试交互 数据库的设计 创建.连接接数据库 前台[表单/分类]   ...

  9. Git&GitHub-初步使用

    Git 1.安装 下载安装包,安装,默认安装了 Git GUI Here 和 Git Bash Here. 需要在哪里使用 git,只需在文件夹空白处右键,选择Git Bash Here即可打开 gi ...

  10. mysql 一看就会 基本语法

    创建表 create table <表名>( <字段名>  类型(长度) not null primary key auto_increment, **主键 name char ...