Chapter 8. Views

Since we're assuming, in this book, that you already know something about SQL Server, I am going to minimize the discussion of the basics and focus primarily on the more meaty uses of views. That said, we'll touch ever so briefly on view basics before moving on.

Views have a tendency to be used either too much, or not enough — rarely just right. When we're done with this chapter, you should be able to use views to:

  • Be more comfortable with view basics

  • Add additional indexing to your database to speed query performance — even when you're not using the view the index is based on

  • Understand and utilize the notion of partitioned views and federated servers

A view is, at its core, really nothing more than a stored query. You can create a simple query that selects from only one table and leaves some columns out, or you can create a complex query that joins several tables and makes them appear as one.

Reviewing View Syntax

The most basic syntax for a view looks something like this:

CREATE VIEW <view name>
AS
<SELECT statement>

It utilizes that basic CREATE <object type> <object name> syntax that exists for most SQL Server objects. It is just the minimum, of course, but it's still all we need in a large percentage of the situations. The more extended syntax looks like this:

CREATE VIEW [<schema name>].<view name> [(<column name list>)]
[WITH [ENCRYPTION] [, SCHEMABINDING] [, VIEW_METADATA]]
AS
<SELECT statement>
[WITH CHECK OPTION]

So, an extremely simple view on the Person.Person ...

Get Professional Microsoft® SQL Server® 2008 Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.