索引:
数据表中数据和相应存储位置的列表
索引的优点:
利用索引可以 提高在表或视图中查找数据的速度
索引的类型有两类,聚集索引和非聚集索引
还有一些其他不常用的,例如唯一索引、索引视图、包含性列索引、
全文索引、XML索引 等
聚集索引:是指表中数据行的物理存储顺序与索引顺序完全相同
非聚集索引: 是指表中数据行的物理存储顺序与索引顺序不完全相同
索引的创建与使用,如下所示:
/*-------------------------------------------索引--------------------------------------------*/
第一种情况:
--判断索引是否存在 存在则删除
if exists(select * from sys.indexes where name= 's_stuinfo_stuname')
drop index stuinfo.s_stuinfo_stuname
go
--创建索引
--create unique(唯一索引) clustered(聚集索引) nonclustered(非聚集索引)
create unique nonclustered
index s_stuinfo_stuname
on stuinfo(stuname)
go
--使用索引
--with(index=索引名称)
select * from StuInfo with(index=s_stuinfo_stuname)
where stuName='张三'
go
第二种情况:
if exists(select *from sys.indexes where name ='s_StuMarks_score')
drop index StuMarks.s_StuMarks_score
go
create nonclustered
index s_StuMarks_score
on stumarks(score)
go
select * from StuMarks with(index=s_StuMarks_score)
where subject='html'
go
视图是一张虚拟表,他用来存放一张或多张表的一部分或综合数据,
其结构和数据是建立在标的基础上的,简单来说就是 查询语句。
视图的创建与使用,如下所示:
/*-----------------------------------------视图------------------------------------------*/
--判断视图是否存在 存在则删除
if exists(select * from sys.views where name ='a_ss')
drop view a_ss
go
--创建视图
create view a_ss
--视图加密
with encryption
as
-- 查询语句
select DENSE_RANK()over(order by sum(score) desc)'排名', StuInfo.stuid 学号,stuname 姓名,SUM(score)as'总成绩'
from StuInfo,StuMarks
where StuInfo.stuid=StuMarks.stuid
group by StuInfo.stuid,StuInfo.stuName
go
select * from a_ss where 姓名='王五'
go
--查询自定义的视图
select * from INFORMATION_SCHEMA.VIEWS