You are on page 1of 5

VIEWS

Contents
Views ....................................................................................................................................................... 1
Creating a view........................................................................................................................................ 1
Updateable views.................................................................................................................................... 2
Example ................................................................................................................................................... 3
References .............................................................................................................................................. 3
Views
The CREATE VIEW statement creates a virtual table whose contents (columns and rows) are defined
by a query. Use this statement to create a view of the data in one or more tables in the database. For
example, a view can be used for the following purposes:
 To focus, simplify, and customize the perception each user has of the database.
 As a security mechanism by allowing users to access data through the view, without granting
the users permissions to directly access the underlying base tables.
 To provide a backward compatible interface to emulate a table whose schema has changed.

A view can be thought of as either a virtual table or a stored query. The data accessible through a view
is not stored in the database as a distinct object. What is stored in the database is a SELECT statement.
The result set of the SELECT statement forms the virtual table returned by the view. A user can use
this virtual table by referencing the view name in Transact-SQL statements the same way a table is
referenced. A view is used to do any or all of these functions:

 Restrict a user to specific rows in a table.

For example, allow an employee to see only the rows recording his or her work in a labor-
tracking table.

 Restrict a user to specific columns.

For example, allow employees who do not work in payroll to see the name, office, work
phone, and department columns in an employee table, but do not allow them to see any
columns with salary information or personal information.

 Join columns from multiple tables so that they look like a single table.

 Aggregate information instead of supplying details.

For example, present the sum of a column, or the maximum or minimum value from a
column.

Creating a view
Views can also be created by right-clicking on the View node under the database in SQL Server
Management Studio and selecting “New View” from the context menu (Figure 1).

1|Page
Figure 1: Creating a view in SQL Server Management Studio

The syntax for creating a view is as follows:

CREATE VIEW [ schema_name . ] view_name [ (column [ ,...n ] ) ]


[ WITH <view_attribute> [ ,...n ] ]
AS select_statement

Views are created by defining the SELECT statement that retrieves the data to be presented by the
view. The data tables referenced by the SELECT statement are known as the base tables for the view.
In this example, titleview in the pubs database is a view that selects data from three base tables to
present a virtual table of commonly needed data:

CREATE VIEW titleview


AS
SELECT title, au_ord, au_lname, price, ytd_sales, pub_id
FROM authors AS a
JOIN titleauthor AS ta ON (a.au_id = ta.au_id)
JOIN titles AS t ON (t.title_id = ta.title_id)

You can then reference titleview in statements in the same way you would reference a table:

SELECT *
FROM titleview

Views are called virtual tables because the result set of a view is us not usually saved in the database.
The result set for a view is dynamically incorporated into the logic of the statement and the result set
is built dynamically at run time.

Updateable views
The data in the underlying base tables can be changed. The following conditions apply:

 Any modifications, including UPDATE, INSERT, and DELETE statements, must reference
columns from only one base table.
 The columns being modified in the view must directly reference the underlying data in the
table columns. The columns cannot be derived in any other way, such as through the
following:
o An aggregate function: AVG, COUNT, SUM, MIN, MAX, GROUPING, STDEV, STDEVP,
VAR, and VARP.

2|Page
o A computation. The column cannot be computed from an expression that uses other
columns. Columns that are formed by using the set operators UNION, UNION ALL,
CROSSJOIN, EXCEPT, and INTERSECT amount to a computation and are also not
updatable.
 The columns being modified are not affected by GROUP BY, HAVING, or DISTINCT clauses.
 TOP is not used anywhere in the select_statement of the view together with the WITH CHECK
OPTION clause.

Example
The SQL for creating a view is as follows:

Create view hiredate_view


AS
select p.FirstName, p.LastName, e.BusinessEntityID, e.HireDate
from HumanResources.Employee e
join Person.Person AS p ON e.BusinessEntityID = p.BusinessEntityID ;

In order to access the data in a view, you can use a standard SELECT statement:

select *
from hiredate_view

References
1. MSDN Library

3|Page

You might also like