视图View
可以隐藏某些权限控制的表、列;
隐藏表的设计背后的复杂性;
提供更加具有统计意义的结果;
提高效率;
1,视图是一个到n个表的字段、聚合等构成的虚拟表。
当表中的值修改了之后,查新view时候,结果也跟着变化;
同时,如果满足一定的条件,修改view中的结果,该列对应的原表中的值也跟着变化;
insert一行到view时,会想对应的原表(们)添加记录;如果涉及到了两个表,那么在view中没有的列
,必须要提供default值等默认处理;
2,
if object_id('tesetView') is not null
drop view tesetView
go
create view tesetView(ID, fid_fk,DBNAME)
as
select ID, fid_fk,DB_NAME() from Table_3
go
delete from Table_3
where ID < 14
select * from tesetView
select * from Table_3
3,如果用了top n,则order by可以使用,否则不行;
4,with check option
在SQL2000中,视图只有select功能,但是到了SQL2005中,视图同时具有了select,insert,update和
delete的功能,假如我们创建一个视图如下(没有with check option选项):
Create view ScoreView
with schemabinding -- Mainly used for security issue.
as
select Name,Subject,Result from dbo.score where Result>80
这个时候如果我们执行 insert into ScoreView values('Bosco','English',71)操作,返回 1 row
affected,表明插入成功,但是我们再执行 select * from ScoreView的时候确看不到刚才添加的记录
如果有了with check option选项,此时,如果用户执行insert into ScoreView values
('Bosco','English',71)操作,(71不满足视图定义的Where Result>80)则SQL Server会返回如下错误
信息给用户:
Msg 550, Level 16, State 1, Line 1
The attempted insert or update failed because the target view either specifies WITH CHECK
OPTION or spans a view that specifies WITH CHECK OPTION and one or more rows resulting from
the operation did not qualify under the CHECK OPTION constraint.
The statement has been terminated.
执行Update、Delete操作会得到同样的结果.