数据库实验实验10 视图与索引
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验10 视图与索引
1.实验目的
(1)掌握视图的创建、修改方法。
(2)能够通过视图修改数据。
(3)理解索引的概念和索引的作用,学会使用索引。
(4)了解聚簇索引和非聚簇索引。
2.实验内容
(1)创建视图
①创建03班学生的视图
create view c1_student
as
select * from SM.dbo.student where clno='03'
②创建03班学生的视图,输出其学号、姓名和年龄
create view c11_student
as
select sno,sname,sage from SM.dbo.student where clno='03'
③创建030002班学生的视图,输出其学号、姓名和年龄,并且更换列名
create view c12_student(学号,姓名,年龄)
as
select sno,sname,sage from SM.dbo.student where clno='030002'
④修改表sutdent的结构,增加一列,再执行上述3组语句,查看结果
??
⑤通过如下两组SQL语句创建星“王”的学生的视图,然后分别向两师徒中插入
一条姓的记录,用SQL语句查看执行结果
create view w1_Student
as
select sno,sname from SM.dbo.student where sname like '王%' with check option
create view s2_Student
as
select sno,sname from SM.dbo.student where sname like '王%'
⑥创建17信计选修了6课程的学生的视图
create view IS_C1(SNo,SName,Score)
as
select student.sno,sname,score from
SM.dbo.student,SM.dbo.SC,SM.dbo.class
where clname='17信计' and student.sno=o and o='6' and student.clno=class.clno
⑦创建17信计选修了6号课程且成绩几个的学生的视图
create view IS_C2
as
select sno,sname,score from IS_C1 where Score>=60
视图可以创建在多张表的基础上,也可以创建在视图上⑧创建一个反应学生出生年份的视图
create view bt_s(sno,sname,sbirth)
as
select sno,sname, 2019-sage from SM.dbo.student
⑨将学生的学号和平均成绩创建一个视图
create view s_g(sno,Gavg)
as
select sno,AVG(score) from SM.dbo.SC group by sno
(2)更新视图定义
①创建所有年龄大于23岁的学生视图
create view s_g1(sno,sage)
as
select sno,sage from SM.dbo.student where sage>23 ②使用select语句查看其结果
select * from s_g1
③将视图s_g1的年龄限定改为20
alter view s_g1
as
select sno,sage from SM.dbo.student where sage>20
(3)删除视图
drop view s_g1
(4)更新视图
①向03班学生的视图C1_student中插入一条记录
insert into c1_student(sno, clno,sname)
values('04002', '030002', '张三')
②使用select语句查看视图c1_student和基本表student
select * from c1_student
select * from SM.dbo.student
③将03班学生的视图c1_student中学号为15148的学生年龄改为23 update c1_student
set sage=23 where sno='15148'
④将03班学生的视图c1_student中学号为15148的学生的记录删除
delete from c1_student where sno='15148'
⑤将上面的两种操作转换为对基表student的修改
(5)在TName列上创建索引
①在对象资源管理器树形目录中展开SM数据库,打开Teacher表的表设计窗口。
②在表中的任意位置上单机鼠标右键,在弹出的快捷菜单中选择“索引/键”命令。
③在“索引/键”对话框中单机“添加”按钮,创建索引。
④在“名称”文本框中输入索引名,在“是唯一的下拉列表框中选择“是”,创建一个唯一性索引,如下图
(6)使用sql语句在表course上创建一个唯一性的聚簇索引,索引排列顺序为降序
use SM
create UNIQUE CLUSTERED index IX_CNo ON Course(CNO DESC)
(7)使用SQL语句在表Course上创建一个非唯一性的非聚簇索引
create nonclustered index IX_CNo IX_CName ON course(cname)
create nonclustered index IX_CNo IX_CName ON course(cnmae) with
drop_existing
(8)查看表Course上的所有索引