索引与视图

这里写图片描述
–创建索引
create nonclustered index ix_StuInfo_stuName
on StuInfo(stuName)

    --1.    在分数表上成绩字段创建非聚集索引index_score,填充因子40%
    create nonclustered index index_score
    on StuMarks(score)
    WITH FILLFACTOR= 40

    --2.    使用index_score索引查询成绩小于80分的同学的学号、姓名、科目和成绩
    select StuInfo.stuid,stuName,subject,score from StuMarks with(index=index_score) ,StuInfo

    where score<80 and StuInfo.stuid=StuMarks.stuid

    --3.删除索引index_score
    if exists (select * from sys.indexes where name ='index_score')
    drop index StuMarks.index_score

    --4.创建视图 View_stuinfo_marks,包括学生学号、姓名、性别、科目和成绩

    create view View_stuinfo_marks
    --加密
    with encryption
    as
    select StuInfo.stuid,stuName,stusex,[subject],score from StuInfo,StuMarks where StuInfo.stuid = StuMarks.stuid;
    go

    --5.    使用视图View_stuinfo_marks查询所有男同学的成绩
    select * from  View_stuinfo_marks where stusex='男'

    --6.    使用视图View_stuinfo_marks查询所有不及格的女同学的学号、姓名及科目

    select stuid,stuName,subject from  View_stuinfo_marks where stusex='女'

    --7.    在原有视图View_stuinfo_marks基础上创建新的加密视图View_stuinfo,包括学号、姓名和总成绩,并按总成绩降序排列
    create view View_stuinfo
    --加密
    with encryption
    as
    select DENSE_RANK() over (order by  SUM(score)desc)
     stuid,stuName,SUM(score) as'总成绩' from  View_stuinfo_marks 
     group by stuid,stuName
    go

    --8.    使用新视图View_stuinfo,显示所有学生的总成绩排名(使用排序函数)


        select * from View_stuinfo


    --9.    删除所有视图


declare @vname varchar(8000) 
set @vname='' 
select @vname=@vname+Name+',' from sysobjects where xtype='V' 
select @vname='drop View '+ left(@vname,len(@vname)-1) 
exec(@vname)
--使用索引
select * from StuInfo with(index=ix_StuInfo_stuName)
where stuid=5
--删除索引
if exists (select * from sys.indexes where name ='ix_StuInfo_stuName')
drop index stuinfo.ix_StuInfo_stuName

–创建视图
create view sumscorerank
–加密
with encryption
as
–查询语句
select * from StuInfo with(index=ix_StuInfo_stuName)
where stuid=5
go

--使用视图

select * from sumscorerank

--非加密状态可查看查询语句

select * from information_schema.views

--删除视图
if exists (select * from sys.views where name ='sumscorerank')
drop view sumscorerank
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值