转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 数据库 >> Sql Server >> 正文
SQL Server Views         

SQL Server Views

作者:闵涛 文章来源:闵涛的学习笔记 点击数:2217 更新时间:2007/11/14 12:57:50
source:
http://odetocode.com/Articles/299.aspx

begin:

SQL Server Views

Posted by scott on 2004年11月28日

An RDBMS uses a view to create a virtual table. The careful use of views can improve the interaction between a .NET application and the underlying data. In this article we will discuss views in Microsoft SQL Server, including best practices for creating and using views.

In SQL Server a view represents a virtual table. Just like a real table, a view consists of rows with columns, and you can retrieve data from a view (sometimes even update data in a view). The fields in the view’s virtual table are the fields of one or more real tables in the database. You can use views to join two tables in your database and present the underlying data as if the data were coming from a single table, thus simplifying the schema of your database for users performing ad-hoc reporting. You can also use views as a security mechanism to restrict the data available to end users. Views can also aggregate data (particularly useful if you can take advantage of indexed views), and help partition data. In this article we will look at these different types of view to see when we can take advantage of a view for our application.

Sample View

The sample database Northwind in SQL Server has a number of views installed by default. One example is the “Current Product List” view, shown here.

  SELECT 
    Product_List.ProductID, Product_List.ProductName
  FROM 
    Products AS Product_List
  WHERE (Product_List.Discontinued = 0)

From inside an application we can issue the following SQL query to retrieve a set of records representing active products.

SELECT ProductID, ProductName from [Current Product List]

The view has created a new virtual table by using records from the Products table and applying a small piece of logic (a filter on the Discontinued field). You could use the view inside of a query from your application, or a stored procedure, or even from inside another view. Views are a simple but powerful abstraction. You can push query complexity, like filter and join statements, into a view to present a simpler model of the data without sacrificing the database design or integrity.

We often describe a view as a virtual table because the database does not store the view data. Instead, when we retrieve data from a view the database engine recreates the data using the SELECT statements in the view’s definition. Since the database only stores a definition of the view, and not the data, there is no significant cost in space for using a view, although there is an exception to this rule we will discuss later in the article. Note also that the database engines query optimizer can often combine the definition of the view with the SQL queries interacting with the view to provide an efficient query plan (in other words, the database engine might not need to perform the entire SELECT operation in the view if it knows the outer query will filter out additional records).

When To Use A View

You need to have a goal in mind when creating a view. There are a number of scenarios where you will want to look for a view as a solution.
  • To hide the complexity of the underlying database schema, or customize the data and schema for a set of users.
  • To control access to rows and columns of data.
  • To aggregate data for performance.
Let’s take a look at each of these scenarios.

Complexity and Customization

Taking care of complex joins and filtering rules inside of a view can benefit other users. As an example, consider the following view from the Northwind database.

CREATE VIEW "Order Details Extended" AS
  SELECT 
     "Order Details".OrderID, 
     "Order Details".ProductID, 
     Products.ProductName, 
     "Order Details".UnitPrice, 
     "Order Details".Quantity, 
     "Order Details".Discount, 
     (CONVERT(money,("Order Details".UnitPrice*Quantity*(1-Discount)/100))*100) AS ExtendedPrice
  FROM 
      Products 
      INNER JOIN 
        "Order Details" ON 
          Products.ProductID = "Order Details".ProductID

A business user with an ad-hoc reporting tool can take advantage of the above view in building customized reports to support her goals. She can use the view to see all of the details about an order without finding the tables to join for product and order information, and without performing the calculation for the price discount. Not only does this make the database easier for the end user, but it also allows a DBA to make changes to the underlying tables without breaking end user reports.

View can also be useful to novice SQL developers on your team. Although stored procedures can remove complexity from a database only a view can be the target of an INSERT, UPATE, or DELETE statement thus functioning more like a real table. There are restrictions to modifying data through views. For more information, see “Modifying Data Through a View” on MSDN. A view can ensure the novice developer always accesses a set of tables in the most efficient manner.

Take care not to let the number of special case views explode in your database. Although it might be tempting to provide every users a view to meet specific filtering requirements, remember it is possible to SELECT from an existing view and provide additional filtering. A large number of views can be difficult to maintain, especially when breaking changes are required to the underlying schema.

Views As A Security Tool

SQL Server already has the ability to restrict access to column in a table (see a previous article on OdeToCode for more information on this topic). However, a view can be a useful tool to provide both column and row level security for a database. Even though you can deny access to the salary column of a payroll table to user Joe, Joe might be confused when he can see the payroll table but has an error appear when his SELECT statement includes the payroll column. A user-friendly solution is to deny Joe any access to the payroll table, but give Joe access to a view that retrieves data from payroll without salary information.

An even more interesting scenario happens when Joe is a department manager and requires access to the salary column, but only for those employees in his department. Row level security is a perfect fit for a solution with a view. You can construct a view to return only payroll records (with salary information) with Joe’s user name in the row (see the built-in SQL Server function USER_NAME()).

Aggregate Views

Another great use case for a view is if you need to roll up or aggregate data from a set of tables, as in the following Northwind view.

CREATE VIEW "Sales by Category" AS
  SELECT 
    Categories.CategoryID, 
    Categories.CategoryName, 
    Products.ProductName, 
    SUM("Order Details Extended".ExtendedPrice) AS ProductSales
  FROM 
    Categories 
      INNER JOIN 
       (Products INNER JOIN 
         (Orders INNER JOIN "Order Details Extended" ON 
            Orders.OrderID = "Order Details Extended".OrderID) 
         ON Products.ProductID = "Order Details Extended".ProductID) 
        ON Categories.CategoryID = Products.CategoryID
  GROUP BY 
    Categories.CategoryID, 
    Categories.CategoryName, 
    Products.ProductName

The view above will give us the sum of the total sales broken down by category and product. If we wanted to see just the total sales for Chai, we could use the follow

[1] [2]  下一页


[Access]sql随机抽取记录  [Access]ASP&SQL让select查询结果随机排序的实现方法
[聊天工具]企业邮件系统的利器----FoxMail Server  [系统软件]OPEN SERVER 5.0.5安装EXP300阵列柜
[系统软件]SQL语句性能优化--LECCO SQL Expert  [系统软件]关于Windows2000Server的灾难恢复
[常用软件][网络]下载服务革命性风暴Poco Server评测  [C语言系列]动态创建SQL Server数据库、表、存储过程等架构信…
[C语言系列]SQL Server到DB2连接服务器的实现  [C语言系列]SQL Server到SYBASE连接服务器的实现
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Sql Server  · MySql
    · Access  · ORACLE
    · SyBase  · 其他
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台