设为首页 收藏本站
查看: 1344|回复: 0

[经验分享] SQL View 的使用语法与原则

[复制链接]

尚未签到

发表于 2016-11-12 03:42:21 | 显示全部楼层 |阅读模式
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

//转载之http://www.cnblogs.com/EricWang/archive/2008/07/09/1238744.html
 

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-298951-1-1.html 上篇帖子: SQL 高级用法 约束,Alter,Create,Drop 下篇帖子: SQL*Plus 下 set sqlprompt 的使用
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表