日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

SQL View 的使用语法与原则

發(fā)布時間:2023/11/29 数据库 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SQL View 的使用语法与原则 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
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這個數(shù)據(jù)庫

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

使用了這個選項(xiàng)之后,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 (一般情況下不要使用這個選項(xiàng))

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創(chuàng)建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里不能進(jìn)行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. 應(yīng)該在確定要用的時候才去創(chuàng)建一個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里可以調(diào)用USER_NAME()來獲得當(dāng)前登錄用戶的信息,從而每個登錄的用戶都回看到不同的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.
???????????????? 創(chuàng)建view的時候要特別留意sql語句的效率。
?? 3. Complexity: to mask complexity from the user
?? 4. Derived Data: to ensure proper data derivation
??????????? view里面可以包含計(jì)算出來的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的代價(jià)
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."
但是對于后臺開發(fā)人員,對view的命名里加入標(biāo)識,可能會有好處。

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的指導(dǎo)原則(什么時候應(yīng)該使用view,一些經(jīng)驗(yàn),命名等7)
Using Views in Microsoft SQL Server, By Craig S. Mullins
http://www.craigsmullins.com/cnr_0299b.htm

轉(zhuǎn)載于:https://www.cnblogs.com/yuquanlaobo/archive/2007/01/19/624465.html

總結(jié)

以上是生活随笔為你收集整理的SQL View 的使用语法与原则的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。