创建简单视图:

use test go create view v1(视图名) as select name from b

这样视图就创建好了.

下面说下视图的本质:

当执行  select * from v1 时,  那么实质上市告诉 sql server 把执行 select name from b 语句返回的结果

给我.

视图就像在命令执行的查询那样运行---没有任何的预先优化过程.这意味着数据在请求和所交付的数据之间增加额额外

的一层系统开销,而视图的运行总是比 执行运行内部的sql 更慢.视图存在的理由就是---对于用户是安全或者简化的.

  • 视图可以隐藏敏感数据,
  • 作为过滤器   例子如下:
               use test
                   go
                   create view v2
                   as
                   select name from b where id=2
    

.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, "Courier New", courier, monospace; background-color: rgba(255, 255, 255, 1) } .csharpcode pre { margin: 0 } .csharpcode .rem { color: rgba(0, 128, 0, 1) } .csharpcode .kwrd { color: rgba(0, 0, 255, 1) } .csharpcode .str { color: rgba(0, 96, 128, 1) } .csharpcode .op { color: rgba(0, 0, 192, 1) } .csharpcode .preproc { color: rgba(204, 102, 51, 1) } .csharpcode .asp { background-color: rgba(255, 255, 0, 1) } .csharpcode .html { color: rgba(128, 0, 0, 1) } .csharpcode .attr { color: rgba(255, 0, 0, 1) } .csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 } .csharpcode .lnum { color: rgba(96, 96, 96, 1) }

总结:视图实际上 只是一个用户不可见的 select语句.

更加复杂的视图:

对视图进行 insert update delete

使用 with check potion 限制插入到视图中的内容。-- 为了通过使用视图更新或者插入数据,结果行必须符合要求

以显示在视图结果中.  也就是插入或者更新的行 必须满足视图中select语句的where条件,

看创建一个视图,sql:

create view v4
as
select name from a
where name like 'a%'
with check option

.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, "Courier New", courier, monospace; background-color: rgba(255, 255, 255, 1) } .csharpcode pre { margin: 0 } .csharpcode .rem { color: rgba(0, 128, 0, 1) } .csharpcode .kwrd { color: rgba(0, 0, 255, 1) } .csharpcode .str { color: rgba(0, 96, 128, 1) } .csharpcode .op { color: rgba(0, 0, 192, 1) } .csharpcode .preproc { color: rgba(204, 102, 51, 1) } .csharpcode .asp { background-color: rgba(255, 255, 0, 1) } .csharpcode .html { color: rgba(128, 0, 0, 1) } .csharpcode .attr { color: rgba(255, 0, 0, 1) } .csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 } .csharpcode .lnum { color: rgba(96, 96, 96, 1) }

那么现在 向视图插入一条数据:

insert into v4(name) values('ss')

.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, "Courier New", courier, monospace; background-color: rgba(255, 255, 255, 1) } .csharpcode pre { margin: 0 } .csharpcode .rem { color: rgba(0, 128, 0, 1) } .csharpcode .kwrd { color: rgba(0, 0, 255, 1) } .csharpcode .str { color: rgba(0, 96, 128, 1) } .csharpcode .op { color: rgba(0, 0, 192, 1) } .csharpcode .preproc { color: rgba(204, 102, 51, 1) } .csharpcode .asp { background-color: rgba(255, 255, 0, 1) } .csharpcode .html { color: rgba(128, 0, 0, 1) } .csharpcode .attr { color: rgba(255, 0, 0, 1) } .csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 } .csharpcode .lnum { color: rgba(96, 96, 96, 1) }

那么sql 会报错:

因为 子句过滤了  a% 内容,而 插入的ss不符合 这个过滤的条件,所以会报错.

如果想知道某一个视图的作用,那么可以用如下方法:

1.

use  test
go
exec sp_helptext v4(视图名)
返回如下信息:

 
2.
select * from sys.sql_modules where [object_id]= object_id('v4')

.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, "Courier New", courier, monospace; background-color: rgba(255, 255, 255, 1) } .csharpcode pre { margin: 0 } .csharpcode .rem { color: rgba(0, 128, 0, 1) } .csharpcode .kwrd { color: rgba(0, 0, 255, 1) } .csharpcode .str { color: rgba(0, 96, 128, 1) } .csharpcode .op { color: rgba(0, 0, 192, 1) } .csharpcode .preproc { color: rgba(204, 102, 51, 1) } .csharpcode .asp { background-color: rgba(255, 255, 0, 1) } .csharpcode .html { color: rgba(128, 0, 0, 1) } .csharpcode .attr { color: rgba(255, 0, 0, 1) } .csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 } .csharpcode .lnum { color: rgba(96, 96, 96, 1) }

结果如下:

加密视图:

新建一个加密的视图:

create view v5
with encryption
as
select name from a
where name like 'a%'
with check option
 
在运行
select * from sys.sql_modules where [object_id]= object_id('v5')

.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, "Courier New", courier, monospace; background-color: rgba(255, 255, 255, 1) } .csharpcode pre { margin: 0 } .csharpcode .rem { color: rgba(0, 128, 0, 1) } .csharpcode .kwrd { color: rgba(0, 0, 255, 1) } .csharpcode .str { color: rgba(0, 96, 128, 1) } .csharpcode .op { color: rgba(0, 0, 192, 1) } .csharpcode .preproc { color: rgba(204, 102, 51, 1) } .csharpcode .asp { background-color: rgba(255, 255, 0, 1) } .csharpcode .html { color: rgba(128, 0, 0, 1) } .csharpcode .attr { color: rgba(255, 0, 0, 1) } .csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 } .csharpcode .lnum { color: rgba(96, 96, 96, 1) }

查看视图时:

.csharpcode, .csharpcode pre { font-size: small; color: rgba(0, 0, 0, 1); font-family: consolas, "Courier New", courier, monospace; background-color: rgba(255, 255, 255, 1) } .csharpcode pre { margin: 0 } .csharpcode .rem { color: rgba(0, 128, 0, 1) } .csharpcode .kwrd { color: rgba(0, 0, 255, 1) } .csharpcode .str { color: rgba(0, 96, 128, 1) } .csharpcode .op { color: rgba(0, 0, 192, 1) } .csharpcode .preproc { color: rgba(204, 102, 51, 1) } .csharpcode .asp { background-color: rgba(255, 255, 0, 1) } .csharpcode .html { color: rgba(128, 0, 0, 1) } .csharpcode .attr { color: rgba(255, 0, 0, 1) } .csharpcode .alt { background-color: rgba(244, 244, 244, 1); width: 100%; margin: 0 } .csharpcode .lnum { color: rgba(96, 96, 96, 1) }

definition变成了空值,无法看到视图到底做了些什么.

如果对视图使用了alter 命令,那么 如果不使用加密,那么 修改后的视图,将变成非加密的.

sql server 2008学习9 视图相关推荐

  1. sql server 2008 学习笔记

    sql server 2008 删除已有的实例 想从setup.exe中区卸载,没找到. 原来还是要从控制面板中卸载,卸载Microsoft SQL Server 2008 卸载界面会提示让你选择要删 ...

  2. sql server 2008学习5 sql基础

    查看数据库的信息: INFORMATION_SCHEMA.CHECK_CONSTRAINTS INFORMATION_SCHEMA.COLUMN_DOMAIN_USAGE INFORMATION_SC ...

  3. sql server 2008学习3 表组织和索引组织

    表组织 表包含在一个或多个分区中,每个分区在一个堆或一个聚集索引结构包含数据行.堆页或聚集索引页在一个或多个分配单元中进行管理,具体的分配单元数取决于数据行中的列类型. 聚集表.堆和索引 SQL Se ...

  4. sql server 2008学习1–系统数据库

    master数据库 数据库记录 SQL Server 系统的所有系统级信息.这包括实例范围的元数据(例如登录帐户).端点.链接服务器和系统配置设置.此外,master 数据库还记录了所有其他数据库的存 ...

  5. sql server 2008学习2 文件和文件组

    数据库文件 每个 SQL Server 数据库至少具有两个操作系统文件:一个数据文件和一个日志文件.数据文件包含数据和对象,例如表.索引.存储过程和视图.日志文件包含恢复数据库中的所有事务所需的信息. ...

  6. sql server 2008学习10 存储过程

    输入输出参数: 给存储过程传参数,叫做输入参数,用户告诉存储过程需要 利用这个参数干些什么. 输出参数: 从存储过程得到那些数据. 创建一个可选参数的存储过程: create proc pa1 @na ...

  7. sql server 2008学习12 事务和锁

    事务 事务的点: 1.begin tran 是事务开始的地方,也是 事务回滚的起点.也就说他会忽略这个起点之后的最终没有提交的所有语句, 2.commit tran 事务的提交 是一个事务的终点 当发 ...

  8. sql server 2008学习8 sql server存储和索引结构

    sql server的存储机制 区段: 是用来为表和索引 分配空间的基本存储单元. 由 8个连续的页面构成,大小为64kb. 区段的注意事项: 一旦区段已满,那么下一记录 将要占据的空间不是记录的大小 ...

  9. sql server 2008学习4 设计索引的建议

    索引设计的建议: 一.检查where子句和连接条件列 当一个查询提交到sql server时,查询优化器尝试为查询中引用的所有表查找最佳的数据访问机制, 一下是它所进行的方式. 1.优化器识别Wher ...

最新文章

  1. VMware ESXi客户端连接控制台时提示“VMRC控制台连接已断开...正在尝试重新连接“的解决方法
  2. 皮一皮:这车是要开上天啊...
  3. springer link:find the journals you need
  4. Python工具包-中文处理工具FoolNLTK
  5. 2018暑假集训---递推递归----一只小蜜蜂hdu2044
  6. HDC,CDC,CClientDC,CPaintDC,CWindowDC的区别
  7. 同步容器和并发容器的区别
  8. 之前画得太丑了,再来张好看的.我试着改小点.但是就看不清了
  9. linux中ps ef和aux,Linux中ps aux、ps -aux、ps -ef之间的区别讲解
  10. 通俗的说这是一个一对多的例子,看看人家是怎么做的!
  11. 查看oracle中的中文所占字节数
  12. Atitit .html5刮刮卡的gui实现总结
  13. kubernetes device or resource busy的问题
  14. VBA 贴片电阻名称转换
  15. CSU 1337 费马大定理
  16. 16g电脑内存有什么好处_16G电脑运行内存可以达到什么样子。
  17. 咖啡馆html报告,咖啡屋调查报告.ppt
  18. 基于DNN-ML的3D全息交互
  19. 扫地机器人水箱背景_智能扫地机器人电控水箱与普通水箱的区别。
  20. 解决微信电脑版备份,手机端出现目前网络状况复杂的问题:当前网络状况复杂,请尝试使用其它网络

热门文章

  1. c++ assert()断言
  2. C语言多种方法实现同一个功能
  3. 稀疏矩阵加法运算_1.2 震惊! 某大二本科生写的矩阵乘法吊打Mathematica-线性代数库BLAS-矩阵 (上)...
  4. python os.urandom 安全_python SystemRandom/os.urandom总是有足够的熵来获得良好的加密
  5. 「Docker」Docker教程+学习笔记
  6. SQL基础操作_3_数据字典(涵盖SQL Server、Oracle、Mysql常见系统数据字典)
  7. Kettle使用_15 文件操作复制文件到结果
  8. Visual Studio Code连接SQL SERVER 2019
  9. 写好的python如何在其它电脑上运行_如何在另一个文件中运行一个python文件?
  10. import caffe失败 No module named caffe