日期:2012-08-08  浏览次数:20792 次

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,
&