10-1 创建视图计算学生课程平均分
现有一个学生数据库,内包含学生表(Student)、课程表(Course)和选修表(SC)。
在每一学年,学生处需要统计每位学生的学习情况,以便进行奖学金评定。请你设计一个视图V_average_grade,统计数据库中课程平均分在80以上的学生。
提示:请使用CREATE VIEW语句作答,并请注意数据表名、列名大小写需与表结构定义一致。
表结构:
学生表(Student)、课程表(Course)和选修表(SC)结构如下:
CREATE TABLE `Student` (
`Sno` varchar(20) NOT NULL,
`Sname` varchar(10) DEFAULT NULL,
`Ssex` varchar(2) DEFAULT NULL,
`Sage` int(3) DEFAULT NULL,
`Sdept` varchar(10) DEFAULT NULL,
PRIMARY KEY (`Sno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `Course` (
`Cno` varchar(10) NOT NULL,
`Cname` varchar(20) DEFAULT NULL,
`Cpno` varchar(10) DEFAULT NULL,
`Ccredit` int(3) DEFAULT NULL,
PRIMARY KEY (`Cno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `SC` (
`Sno` varchar(20) NOT NULL,
`Cno` varchar(10) NOT NULL,
`Grade` int(3) DEFAULT NULL,
PRIMARY KEY (`Sno`,`Cno`),
KEY `Cno` (`Cno`),
CONSTRAINT `sc_ibfk_1` FOREIGN KEY (`Sno`) REFERENCES `student` (`Sno`),
CONSTRAINT `sc_ibfk_2` FOREIGN KEY (`Cno`) REFERENCES `course` (`Cno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
表样例
Student
表:
Course
表:
SC
表:
输出样例:
视图V_average_grade
输出:
create view V_average_grade
as
select
Student.Sdept,
Student.Sno,
Student.Sname,
avg(SC.Grade) as Average_grade
from
Student
join SC on SC.Sno = Student.Sno
group by Student.Sdept,Student.Sno,Student.Sname
having
avg(SC.Grade)>80;
10-2 创建视图查找不及格学生
现有一个学生数据库,内包含学生表(Student)、课程表(Course)和选修表(SC)。
每学期末,教务处要安排课程补考或者重修,因此需要统计本学期课程考试不合格的学生、课程、成绩。假设选修表中课程成绩小于60的同学都需要补考。请你设计一个视图V_FailedCourseStudent,统计数据库中课程成绩小于60的学生。
提示:请使用CREATE VIEW语句作答,并请注意数据表名、列名大小写。
表结构:
学生表(Student)、课程表(Course)和选修表(SC)结构如下:
CREATE TABLE `Student` (
`Sno` varchar(20) NOT NULL,
`Sname` varchar(10) DEFAULT NULL,
`Ssex` varchar(2) DEFAULT NULL,
`Sage` int(3) DEFAULT NULL,
`Sdept` varchar(10) DEFAULT NULL,
PRIMARY KEY (`Sno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `Course` (
`Cno` varchar(10) NOT NULL,
`Cname` varchar(20) DEFAULT NULL,
`Cpno` varchar(10) DEFAULT NULL,
`Ccredit` int(3) DEFAULT NULL,
PRIMARY KEY (`Cno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `SC` (
`Sno` varchar(20) NOT NULL,
`Cno` varchar(10) NOT NULL,
`Grade` int(3) DEFAULT NULL,
PRIMARY KEY (`Sno`,`Cno`),
KEY `Cno` (`Cno`),
CONSTRAINT `sc_ibfk_1` FOREIGN KEY (`Sno`) REFERENCES `student` (`Sno`),
CONSTRAINT `sc_ibfk_2` FOREIGN KEY (`Cno`) REFERENCES `course` (`Cno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
表样例
Student
表:
Course
表:
SC
表:
输出样例:
视图V_FailedCourseStudent
输出:
create view V_FailedCourseStudent as
select
Student.Sdept,
Student.Sno,
Student.Sname,
Course.Cname,
SC.Grade
from Student
join SC on SC.Sno=Student.Sno
join Course on Course.Cno = SC.Cno
where SC.Grade<60;
10-3 创建分组统计视图
创建每个专业学生修课信息的视图PerView,包括每个专业的专业名称、修课的学生人数、平均成绩。
表结构:
create table Student(
sno char(8) primary key,
sname varchar(10) not null,
gender char(2) check(gender='男' or gender='女'),
birthdate date,
major varchar(20) default '软件工程'
);
create table SC(
scid int auto_increment primary key,
sno char(8) references Student(sno),
cno char(10) references Course(cno),
tno char(15) references Teacher(tno),
grade int check(grade>=0 and grade<=100),
gpoint decimal(2,1),
memo text(100)
);
表样例
Student
表:
sno | sname | gender | birthdate | major |
---|---|---|---|---|
21012101 | 李勇 | 男 | 2005-10-20 | 计算机科学 |
21012102 | 刘晨 | 男 | 2006-5-5 | 计算机科学 |
21012103 | 王晓敏 | 女 | 2005-10-6 | 计算机科学 |
21021101 | 李佳睿 | 男 | 2006-3-30 | 软件工程 |
21021102 | 吴宾 | 男 | 2005-9-21 | 软件工程 |
21021103 | 张海 | 男 | 2005-10-20 | 软件工程 |
21031101 | 钱晓萍 | 女 | 2006-6-1 | 网络工程 |
21031102 | 王大力 | 男 | 2005-11-15 | 网络工程 |
21041101 | 于洋 | 男 | 2006-3-15 | 数据科学 |
21041102 | 郭霖 | 男 | 2006-3-2 | 数据科学 |
SC
表:
scid | sno | cno | tno | grade | gpoint | memo |
---|---|---|---|---|---|---|
null | 21012101 | c01 | t200306m12132 | 90 | null | null |
null | 21012101 | c02 | t200703m12218 | 86 | null | null |
null | 21012101 | c03 | t200703m12218 | null | null | 缺考 |
null | 21012102 | c02 | t200703m12218 | 78 | null | null |
null | 21012102 | c03 | t200703m12218 | 66 | null | null |
null | 21021102 | c01 | t200306m12132 | 82 | null | null |
null | 21021102 | c02 | t200608f12205 | 75 | null | null |
null | 21021102 | c03 | t200306m12132 | null | null | 缓考 |
null | 21021102 | c05 | t201803f12405 | 50 | null | null |
null | 21021103 | c02 | t200703m12218 | 68 | null | null |
null | 21021103 | c04 | t201208m12308 | 92 | null | null |
null | 21031101 | c01 | t200306m12132 | 80 | null | null |
null | 21031101 | c02 | t200608f12205 | 95 | null | null |
null | 21041102 | c02 | t200608f12205 | 56 | null | null |
null | 21041102 | c05 | t201803f12405 | 88 | null | null |
输出样例:
PerView
视图:
create view PerView as
select
Student.major as 专业名,
count(distinct Student.sno) as 修课人数,
avg(SC.grade) as 平均成绩
from Student
join SC on SC.sno=Student.sno
where SC.grade is not null
group by Student.major
10-4 从视图PerView中查询数据。
从上题中创建的视图PerView中查询平均成绩超过75分的专业有哪些。
PerView视图结构:
Create view PerView(专业名, 修课人数, 平均成绩)
AS Select major, count(distinct sc.sno), avg(grade) from student join sc on student.sno=sc.sno group by major;
PerView视图数据样例
PerView
视图:
专业名 | 修课人数 | 平均成绩 |
---|---|---|
数据科学 | 1 | 72.0 |
网络工程 | 1 | 87.5 |
计算机科学 | 2 | 80.0 |
软件工程 | 2 | 73.4 |
输出样例:
专业名 | 平均成绩 |
---|---|
网络工程 | 87.5 |
计算机科学 | 80.0 |
select 专业名,平均成绩
from PerView
where 平均成绩>75
10-5 创建带表达式的视图StuView
已知学生表Student
,创建学生信息的视图StuView
,包括学生学号、姓名和年龄,在视图中的列名分别为No,Name和Age。
表结构:
create table Student(
sno char(8) primary key,
sname varchar(10) not null,
gender char(2) check(gender='男' or gender='女'),
birthdate date,
major varchar(20) default '软件工程'
);
表样例
Student
表:
输出样例:
注意:年龄字段会随年份不同而变化,这里只是针对当前时间点得到的情况
create view StuView as
select
sno as No,
sname as Name,
year(getdate())-year(birthdate) as Age
from Student