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

时间: 2024-11-10 00:57:14

SQL View 的使用语法与原则的相关文章

SQL-W3School-高级:SQL VIEW(视图)

ylbtech-SQL-W3School-高级:SQL VIEW(视图) 1.返回顶部 1. 视图是可视化的表. 本章讲解如何创建.更新和删除视图. SQL CREATE VIEW 语句 什么是视图? 在 SQL 中,视图是基于 SQL 语句的结果集的可视化的表. 视图包含行和列,就像一个真实的表.视图中的字段就是来自一个或多个数据库中的真实的表中的字段.我们可以向视图添加 SQL 函数.WHERE 以及 JOIN 语句,我们也可以提交数据,就像这些来自于某个单一的表. 注释:数据库的设计和结构

Oracle中PL/SQL简介、基本语法以及数据类型

Oracle中PL/SQL简介.基本语法以及数据类型 一.PL/SQL简介. Oracle PL/SQL语言(Procedural Language/SQL)是结合了结构化查询和Oracle自身过程控制为一体的强大语言,PL/SQL不但支持更多的数据类型,拥有自身的变量申明,赋值语句,而且还有条件,循环等流程控制语句.过程控制结构与SQL数据处理能力无缝的结合形成了强大的编程语言,可以创建过程和函数以及程序包. PL/SQL是一种块结构的语言,它将一组语句放在一个块中,一次性的发送给服务器,由服

SQL VIEW 使用语法

之前一直都不知道VIEW有什么作用,写程序的时候也很少遇到过,复习SQL语句的时候碰到了,就记录下来吧. 什么是视图? 在 SQL 中,视图是基于 SQL 语句的结果集的可视化的表. 视图包含行和列,就像一个真实的表.视图中的字段就是来自一个或多个数据库中的真实的表中的字段.我们可以向视图添加 SQL 函数.WHERE 以及 JOIN 语句,我们也可以提交数据,就像这些来自于某个单一的表. 注释:数据库的设计和结构不会受到视图中的函数.where 或 join 语句的影响. SQL CREATE

SQL VIEW(视图)

视图是可视化的表. 本章讲解如何创建.更新和删除视图. SQL CREATE VIEW 语句 什么是视图? 在 SQL 中,视图是基于 SQL 语句的结果集的可视化的表. 视图包含行和列,就像一个真实的表.视图中的字段就是来自一个或多个数据库中的真实的表中的字段.我们可以向视图添加 SQL 函数.WHERE 以及 JOIN 语句,我们也可以提交数据,就像这些来自于某个单一的表. 注释:数据库的设计和结构不会受到视图中的函数.where 或 join 语句的影响. SQL CREATE VIEW

SQL Server 常用高级语法笔记

1.case...end (具体的值)case后面有值,相当于c#中的switch case注意:case后必须有条件,并且when后面必须是值不能为条件. -----------------case--end---语法结构--------------------- select name , --注意逗号 case level --case后跟条件 when 1 then '骨灰' when 2 then '大虾' when 3 then'菜鸟' end as'头衔' from [user]

SQL 创建索引,语法

--unique唯一索引,clustered聚集索引,nonclustered非聚集索引 .主键是唯一的,所以创建了一个主键的同时,也就这个字段创建了一个唯一的索引.SQL SERVER将主键默认定义为聚集索引,事实上,索引是否唯一与是否聚集是不相关的,聚集索引可以是唯一索引,也可以是非唯一索引: 唯一索引实际上就是要求指定的列中所有的数据必须不同 /* 主键一唯一索引的区别: 1 一个表的主键只能有一个,而唯一索引可以建多个. 2 主键可以作为其它表的外键. 3 主键不可为null,唯一索引可

SQL中的JOIN语法详解

参考以下两篇博客: 第一个是 sql语法:inner join on, left join on, right join on详细使用方法 讲了 inner join, left join, right join的意义和用法. 第二个是 SQL中的left outer join,inner join,right outer join用法详解 讲了关系运算背后的数学原理,以及提到了更多类型的连接操作: inner join- 笛卡尔乘积再选取, left outer join, right out

PL/SQL简介与基本语法

PL/SQL的简介: PLSQL 是Oracle公司在SQL基础上进行扩展而成的一种过程语言.PLSQL提供了典型的高级语言特 性,包括封装,例外处理机制,信息隐藏,面向对象等:并把最新的编程思想带到了数据库服务器和工具 集中. 与Java, C#相比 ,PLSQL的优势是:SQL语言可以直接写到PLSQL的"块"中或者是PLSQL的过程. 函数中.没有必要向java那样先创建Statement对象来执行SQL; 这使得PLSQL成为很强大的事务处理语 言,即:使用SQL来处理数据,使

web day15 数据库概述,MySQL,SQL语句,数据查询语法DQL

数据库管理系统(DBMS)的概述 1. 什么是DBMS:数据的仓库 > 方便查询 > 可存储的数据量大 > 保证数据的完整.一致 > 安全可靠 2. DBMS的发展:今天主流数据库为关系型数据库管理系统(RDBMS 使用表格存储数据) 3. 常见DBMS:Orcale.MySQL.SQL Server.DB2.Sybase 4. DBMS = 管理程序 + 多个数据库(DB) 5. DB = 多个table(不只是table,但这里先不介绍其他组成部分) 6. table的结构(即