SQL View 的使用语法与原则

1.
View只是存储下来的sql 语句
Views are nothing but saved SQL statements, and are sometimes referred as “Virtual Tables”.
Keep in mind that Views cannot store data (except for Indexed Views); rather they only refer to data present in tables.

2.
create a view

USE Northwind // 使用Northwind这个数据库

GO

CREATE VIEW vwSample

As

SELECT CustomerID, CompanyName, ContactName FROM CUSTOMERS

GO


use a view

SELECT * from vwSample


drop a view

DROP VIEW vwSample


3.

Creating Views with the SCHEMABIND Option

使用了这个选项之后,view里面引用到的表的shema就不能被改变了。

Creating a view with the SCHEMABINDING option locks the tables being referred by the view and prevents any changes that may change the table schema.

Notice two important points while creating a view with SCHEMABINDING OPTION:

  • The objects should be referred to by their owner names [two part name].
     
  • SELECT * is not permitted.

Here’s an example for a view with the SCHEMABIND OPTION:

CREATE VIEW vwSample

With SCHEMABINDING

As

SELECT

          CustomerID,

          CompanyName,

          ContactName

FROM DBO.CUSTOMERS -- Two part name [ownername.objectname]

GO


4.

Creating Views with the ENCRYPTION Option (一般情况下不要使用这个选项)

This option encrypts the definition of the view. Users will not be able to see the definition of the View after it is created.

USE NORTHWIND

GO

CREATE VIEW vwSample

With ENCRYPTION

As

SELECT

          CustomerID,

          CompanyName,

          ContactName

FROM DBO.CUSTOMERS

GO

SELECT *

FROM SYSCOMMENTS

WHERE ID FROM SYSOBJECTS WHERE XTYPE = ‘V’ AND

The view definition will be stored in an encrypted format in the system table named ‘syscomments’.


5.

Indexed Views

给一个view创建index

SQL SERVER 2000 allows an index to be created on a View. Wow! Previous versions of SQL SERVER will not allow you to do this. But one important point to be noted here is that the first index on the View should be a UNIQUE CLUSTERED INDEX only. SQL SERVER 2000 will not allow you to create any other INDEX unless you have an UNIQUE CLUSTERED INDEX defined on the view.

Let’s check out a sample example for an Indexed View:

CREATE VIEW vwSample

As

SELECT

          CustomerID,

          CompanyName,

          ContactName

FROM DBO.CUSTOMERS

GO

CREATE UNIQUE CLUSTERED INDEX indClustered

ON NORTHWIND.DBO.VWSAMPLE (CUSTOMERID)

GO

The above statement will create a unique clustered index on the View.


6.
Almost any SQL that can be issued natively can be coded into a view; there are exceptions, however. For example, the UNION operator can not be used in a view and you cannot create a trigger on a view.
view里不能进行union操作

7.
The text of any view can be retrieved from the SQL Server system catalog using the system procedure sp_helptext (unless the view was created specifying WITH ENCRYPTION). For example, this statement:
  使用命令来查看view的定义(也可以用这个命令来查看stored procedure的定义)
sp_helptext Sample_view
 
Might return the following output:
 
Text
CREATE VIEW Sample_View
AS SELECT title, au_fname, au_lname
FROM titles, titleauthor, authors
WHERE titles.title_id=titleauthor.title_id
AND authors.author_id=titleauthor.author_id

 
It is also possible to rename a view using the system procedure sp_rename.


8. 应该在确定要用的时候才去创建一个view。
Views should be created only when they achieve a specific, reasonable goal. Each view should have a specific application or business requirement that it fulfills before it is created. That requirement should be documented somewhere, preferably in a data dictionary or repository.
There are seven primary uses for which views excel. These are:

1. Security: to provide row and column level security
note: view里可以调用USER_NAME()来获得当前登录用户的信息,从而每个登录的用户都回看到不同的view
   2. Efficient Access: to ensure efficient access paths
                 The use of proper join criteria and predicates on indexed columns can be coded into the view.
                 创建view的时候要特别留意sql语句的效率。
   3. Complexity: to mask complexity from the user
   4. Derived Data: to ensure proper data derivation
            view里面可以包含计算出来的column,方便使用。
5. Domain Support: to provide domain support
A domain basically identifies the valid range of values that a column can contain.
Some of the functionality of domains can be implemented using views and the WITH CHECK OPTION clause.
6. Column Renaming:  to rename columns, and
7. Single Solution View:to provide solutions which can not be accomplished without views
The final view usage situation might actually be the most practical usage for views—when views are the only solution!

9.
do not needlessly create SQL Server objects that are not necessary.
使用view的代价
In terms of views, for every unnecessary view that is created SQL Server will insert rows into the following system catalog tables: syscolumns, syscomments, sysdepends, sysobjects, sysprocedures, and sysprotects. If uncontrolled view creation is permitted, disk usage will increase, I/O problems can occur, and inefficient catalog organization may result.

10. View Naming convention
" Therefore, it stands to reason that views should utilize the same naming conventions as are used for tables."
但是对于后台开发人员,对view的命名里加入标识,可能会有好处。

11.
Always Specify Column Names
When creating views SQL Server provides the option of specifying new column names for the view or defaulting to the same column names as the underlying base table(s). It is always advisable to explicitly specify view column names instead of allowing them to default,


Reference
介绍view的基本语法
http://www.sql-server-performance.com/nn_views.asp

(好文章,推荐!)使用view的指导原则(什么时候应该使用view,一些经验,命名等7)
Using Views in Microsoft SQL Server, By Craig S. Mullins
http://www.craigsmullins.com/cnr_0299b.htm

转载于:https://www.cnblogs.com/yuquanlaobo/archive/2007/01/19/624465.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/408059.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

What is AJAX?(转)(二)

关键词: ajax Asynchronous JavaScript Xml 目录 什么是AJAX? 第一步:如何发出一个HTTP请求 第二步:处理服务器的响应 第三步:一个简单的例子 第四步&#xf…

react学习(68)--ant design inputNumber

<InputNumber min{0} style{{ width: 100 }} onPressEnter{this.handleSearch} />

控制`Actor`朝向,运动 Learn Unreal Engine (with C++)

控制Actor的朝向,以及Actor的运动 SpaceshipBattle fanxingin/UE4项目 - 码云 - 开源中国 (gitee.com) 控制Actor朝向鼠标 设置鼠标在游戏中可见 获取玩家控制器鼠标可见设置为true PC Cast<APlayerController>(GetController()); PC->bShowMouseCursor true;获取…

zz 聊聊并发(一)

引言 在多线程并发编程中synchronized和Volatile都扮演着重要的角色&#xff0c;Volatile是轻量级的synchronized&#xff0c;它在多处理器开发中保证了共享变量的“可见性”。可见性的意思是当一个线程修改一个共享变量时&#xff0c;另外一个线程能读到这个修改的值。 它在某…

小故事的蕴意1

①蛇与乌龟的故事   一条大蟒蛇和一条小毒蛇是朋友。   这天他们在路边发现了一只巨大的乌龟。   蛇兄弟想&#xff0c;这么大的个儿&#xff0c;可是一顿美餐啊。   蟒蛇说&#xff1a;我来对付他。   于是蟒蛇施展自己的绝技&#xff0c;用身体将大乌龟牢牢地缠住。…

.Net开发人员应该下载的十种必备工具(三)

NDoc 编写代码文档资料几乎总是一项令人畏惧的任务。我所说的不是早期设计文档&#xff0c;甚至也不是更为详细的设计文档&#xff1b;我说的是记录类上的各个方法和属性。NDoc 工具能够使用反射来分析程序集&#xff0c;并使用从 C# XML 注释生成的 XML 自动为代码生成文档资料…

react学习(69)--置空操作

//重置按钮handleReset () > {this.props.form.setFieldsValue({merchantCode: ,goodsCode: ,});};

Actor范围内随机生成 Learn Unreal Engine (with C++)

Actor范围内随机生成 Learn Unreal Engine (with C) SpaceshipBattle fanxingin/UE4项目 - 码云 - 开源中国 (gitee.com) Actor范围内随机生成 新建box组件 SpawnArea CreateDefaultSubobject<UBoxComponent>(TEXT("SpawnArea"));RootComponent SpawnArea…

浏览器是如何展示网页的

作为一名前端工程师&#xff0c;我们应该清楚浏览器是如何展示网页的&#xff0c;了解浏览器的原理可以令设计者找到适合的途径把网页展示给用户。 首先&#xff0c;让我们来看看什么是浏览器。网页浏览器是显示网页服务器或档案系统内的文件&#xff0c;并让用户与这些文件互动…

.Net开发人员应该下载的十种必备工具(二)

NUnit NUnit 是为 .NET 框架生成的开放源代码单元测试框架。NUnit 使您可以用您喜欢的语言编写测试&#xff0c;从而测试应用程序的特定功能。当您首次编写代码时&#xff0c;单元测试是一种测试代码功能的很好方法&#xff0c;它还提供了一种对应用程序进行回归测试的方法。NU…

金山毒霸2007亮点介绍

盘点即将过去的2006年&#xff0c;计算机病毒多变种&#xff0c;恶意软件、流氓软件的泛滥&#xff0c;钓鱼欺诈网站的频繁出现&#xff0c;都给网民留下深刻印象。为在新的一年里对杀毒市场展开新一轮争夺&#xff0c;主流杀毒软件也纷纷发布了新一代产品。纵观各厂商提供的解…

子弹创建及发射 Learn Unreal Engine (with C++)

子弹创建及发射 Learn Unreal Engine (with C) SpaceshipBattle fanxingin/UE4项目 - 码云 - 开源中国 (gitee.com) 子弹的创建 声明: UPROPERTY(EditAnywhere, Category "Fire")TSubclassOf<ABullet> Bullet;实现: //在空组件处生产子弹GetWorld()->…

爬虫工作量由小到大的思维转变---<第二十四章 Scrapy的`统计数据`收集stats collection>

前言: 前两篇是讲的数据诊断分析,还有一篇深挖解决内存泄漏的文章,目前我还没整理汇编出来;但是,想到分析问题的时候,忽然觉得爬虫的数据统计好像也挺重要;于是,心血来潮准备来插一篇这个------让大家对日常scrapy爬的数据,做到心里有数!不必自己去搅破脑汁捣腾日志,敲计算器了…

uva 10245 The Closest Pair Problem_枚举

题意&#xff1a;求任意两点之间的距离的最少一个距离 思路&#xff1a;枚举一下就可以了 #include <iostream> #include<cstdio> #include<cmath> using namespace std; #define N 10010 struct node{double x,y; }p[N]; int main(int argc, char** argv) {…

react学习(70)--拼接方式

const tabs [{ key: , value: 全部 }, ...MERCHANTLISTSTARTUS];

.Net开发人员应该下载的十种必备工具(一)

用于编写单元测试的 NUnit用于创建代码文档资料的 NDoc用于生成解决方案的 NAnt用于生成代码的 CodeSmith用于监视代码的 FxCop用于编译少量代码的 Snippet Compiler两种不同的转换器工具&#xff1a;ASP.NET 版本转换器和 Visual Studio .NET 项目转换器用于生成正则表达式的 …

旋转根组件 Learn Unreal Engine (with C++)

旋转根组件 Learn Unreal Engine (with C) 在UE4中,根组件是无法旋转定位的,只能够缩放,在一些情况下,我们有旋转根组件的需求 SpaceshipBattle fanxingin/UE4项目 - 码云 - 开源中国 (gitee.com) 旋转根组件 将SceneComponent设为根组件 然后将StaticMeshComponentattach…

2007.2.14 日程安排

公元二零零七年二月十四日&#xff0c;农历腊月二十七&#xff0c;该天尤为特别&#xff0c;乃春节长假之初始。此外&#xff0c;该天将是片地鸳侣&#xff0c;漫天桃花之好时日&#xff0c;于是吾将广纳四方真气&#xff0c;闭关修炼&#xff0c;与世无争。00&#xff1a;00 -…

react学习(71)--render使用

title: 品牌资质有效期,dataIndex: certificationStartDate,render: (text, row) > {return (<span>{moment(row.certificationStartDate).format(YYYY-MM-DD)}-{moment(row.certificationEndDate).format(YYYY-MM-DD)}</span>);},

2014.3.5-C语言学习小结

知识点:1.结构体 struct2.联合体 union3.枚举4.结构、联合与函数结构体思考:如果现在希望保存一个学生的信息,该如何保存sprintf "zhangsan:18:180”%s:%d:%d, name, height, agechar name[10][100]int age[10]int height[10]1.什么是结构体 struct结构体指的是一种数据结…