2020-10-28

索引     视图

 

 

create database student
go
use student
go

create table UserInfo
(
stuID int primary key identity(1,1),  --学生id
stuName varchar(20) not null,         --学生姓名
sturoom   int not null,               --学生班级
stusex  varchar(20) not null,         --学生性别
)

create table Product
(
stuID int references UserInfo(stuID) not null,    --学生id
score  int  not null,                             --考试成绩
stubject    varchar(20) not null,                 --考试科目
)

insert into UserInfo values('张三',1,'男')
insert into UserInfo values('李四',2,'男')
insert into UserInfo values('王五',3,'女')

insert into Product values(1,80,'html')
insert into Product values(2,90,'jave')
insert into Product values(3,100,'c#')


select   *  from  UserInfo

select  *  from   Product

select * from UserInfo a inner join Product b on a.stuID=b.stuID

 

 

 

 


--如果存在该索引   先将其删除掉
--if   exists(select *  from  sys.indexes  where  name='IX_score')

--drop  index   Product   IX_score

--对成绩表字段创建非聚集索引    填充因子
--craete   nonclustered   index   IX_score  on  Product(score)   with   fillfactor=30

--指定按索引查询
--select  *  from Product  with  (index=IX_score)

--where   score   between  60  and  90

 

 

 

--创建名为 view_UserInfo_Product 的视图

--create   view    view_UserInfo_Product(stuID,stuName,score)

--AS

--select  UserInfo.stuID,stuName  from    UserInfo  left   join Product  on

-- UserInfo.stuID=Product.stuID
-- go
--使用视图

-- select  *  from   view_UserInfo_Product

 

 


--命名一般按照view_数据对应的表的名字
--视图能够进行嵌套,最多能嵌套32层,最多能包含1024个字段
--视图中不能出现default  关键字   compute和  compute  by  ,into等
--视图中不能进行order  by   排序语句,如果要出现排序语句必须在查
--询语句前面加上top关键字

--if  exists(select  *  from   sys.all_objects  where  name='view_UserInfo_Product')
--drop  view   view_UserInfo_Product

--go

--create   view   view_UserInfo_Product

--with  view  view_UserInfo_Product

--with   encryption 

--as

--select   top  100  Product  as '考试编号',  UserInfo.stuID  as '学号',   stubject,score ,stuName  from   UserInfo,Product  where   UserInfo.stuID=Product.stuID    order  by   UserInfo.stuID  desc 
--go

 

 

 


--命名一般按照view_数据对应的表的名字
--视图能够进行嵌套,最多能嵌套32层,最多能包含1024个字段
--视图中不能出现default  关键字   compute和  compute  by  ,into等
--视图中不能进行order  by   排序语句,如果要出现排序语句必须在查
--询语句前面加上top关键字

--if  exists(select  *  from   sys.all_objects  where  name='view_UserInfo_Product')
--drop  view   view_UserInfo_Product

--go

--create   view   view_UserInfo_Product

--with  encryption

--as

--select  top 100  select   top  100  Product  as '考试编号',  UserInfo.stuID  as '学号',   stubject,score ,stuName  from   UserInfo,Product  where   UserInfo.stuID=Product.stuID    order  by   UserInfo.stuID  desc 
--go

 

 

 

 

--使用 with   encryption参数为视图加密  
--create  view    view_UserInfo_Product

--with   encryption  
--as

--select   stuName,UserInfo.stuID  from   UserInfo    left   join   Product   on  

-- UserInfo.stuID=Product.stuID 


-- go