当前位置:文档之家› 视图、索引与数据库关系-数据库实验九

视图、索引与数据库关系-数据库实验九

广州xx学院
数据库实验报告
专业班级计算机181 实验日期2020.5.2 姓名李x 学号20181x
实验名称视图、索引与数据库关系指导教师王x
(报告内容包括实验目的、实验设备及器材、实验内容、实验步骤、实验结果、实验小结等)
一、实验目的
要求学生掌握SQL server中的视图创建、查看、修改、和删除的方法;掌握索引的创建和删除的方法以及数据库关系图的实现方法;加深对视图和SQL server数据库关系图作用的理解。

二、实验设备及器材
Windows平台,SQL server 2012
三、实验内容
(1)创建、查看、修改和删除视图。

(2)创建、删除索引文件。

(3)创建数据库关系图。

四、实验步骤
1.在数据库EDUC中创建下列视图。

在EDUC数据库中以学生信息表Student、Course和SC表为基础完成下列视图的创建。

(1)创建计算机系学生基本情况视图V_Computer。

CREATE VIEW V_Computer
AS
SELECT*
FROM Student
WHERE dno='D1'
D1为计算机系的编号
(2)创建Student、Course和SC表中学生的sno,sname,tcid,cname,score创建视图V_SC_G。

create view V_SC_G
as
select Student.sno,sname,SC.tcid,cname,SC.score
from Student,Course,SC,TC
where Student.sno=SC.sno and SC.tcid=TC.tcid and
o=o
with check option;
(3)将各系学生人数、平均年龄创建视图V_NUM_AVG。

create view V_NUM_AVG
as
select count(sno)人数,avg(YEAR(GETDATE())-YEAR(birthday))平均年龄,dname from Student,Dept
where Student.dno=Dept.dno
group by dname;
(4)创建一个反映学生出生年份的视图V_YEAR。

create view V_YEAR
as
select birthday
from Student
(5)将各学生选修课程的门数及平均成绩创建视图V_AVG_S_G。

create view V_AVG_S_G
as
select SC.sno,count(o)课程门数,avg(SC.score)平均成绩
from Student,SC,Course,TC
where student.sno=sc.sno and SC.tcid=TC.tcid
and o=o
group by SC.sno;
(6)将各门课程的选修人数及平均成绩创建视图V_AVG_C_G。

create view V_AVG_C_G
as
select o,count(o)课程选修人数,avg(SC.score)平均成绩
from SC,TC,Course
where SC.tcid=TC.tcid and o=o
group by o;
2.利用视图完成下列查询。

(1)查询平均成绩为90分以上的学生的学号、姓名和成绩。

use EDUC
select V_SC_G.sno,V_SC_G.sname,V_AVG_S_G.平均成绩
from V_SC_G,V_AVG_S_G
where V_SC_G.sno=V_AVG_S_G.sno and平均成绩>90
(2)查询各科成绩均高于平均成绩的学生的学号、姓名、课程、和成绩。

use EDUC
select sno,sname,cname,score
from V_SC_G
GROUP BY sno,sname,cname,score
having score>avg(score)
(3)按系统计各系平均成绩在80分以上的人数,结果按降序排列。

use EDUC
select dno,count(avg(score))Num
from V_SC_G,Student
where Student.sno=V_SC_G.sno
GROUP BY dno
having avg(score)>80
order by Num desc
3.基于EDUC数据库完成下面的操作。

(1)对教师表Teacher中的教师号tno创建聚簇索引,并按降序排列。

use EDUC
create index IND_tno
on Teacher(tno desc)
(2)对学生成绩信息表SC先按上课编号tcid升序排序,再按学生成绩score降序排列创建索引。

use EDUC
create index IND_tcid_score
on SC(tcid asc,score desc)
(3)对课程信息表Course中的课程编号创建唯一索引,并按升序排列。

use EDUC
create unique index IND_cno
on Course(cno asc)
4.在数据库TSGL中创建下列视图。

(1)创建视图Read_Borrow_Book,字段为ReaderID,,BorrowerDate, BookID,,满足条件BorrowedQuantity>3。

create view Read_Borrow_Book
as
select readers.ReaderID,,borrowinf.BorrowedDate,books.BookID
from readers,books,borrowinf
where BorrowedQuantity>3
(2)利用books表创建字段为BookID,Name,Author,Publisher,PublisherDate的视图,条件满足“出版社”=“清华大学出版社”,并且Price>30。

create view IND_Book
as
select BookID,Name,Author,Publisher,PublisherDate
from books
where Publisher='清华大学出版社'and Price>30
(3)创建视图borrow_inf,字段为RedaerID,Name,RederType,BorrowedDate,条件为ReturnDate为空。

create view borrow_inf
as
select readers.ReaderID,Name,RederType,BorrowedDate
from readers,borrowinf
where ReturnDate=''
五、实验小结
通过此次实验,我已经基本掌握SQL server中的视图创建、查看、修改、和删除的方法,以及掌握索引的创建和删除的方法以及数据库关系图的实现方法。

相关主题