You are on page 1of 27

Module 8

Designing and Implementing Views


Module Overview

Introduction to Views
Creating and Managing Views
• Performance Considerations for Views
Lesson 1: Introduction to Views

What Is a View?
Types of Views
Advantages of Views
Dynamic Management Views
Other System Views
• Demonstration: Querying Catalog Views and
DMVs
What Is a View?

• A view is a stored query expression:


• It behaves like a table, but the data is stored in the
underlying tables
• It has a name
• It can be referenced by other queries

• Filter records by restricting the columns or rows


that are returned
• Use views to:
• Simplify the complex underlying relationship between
tables
• Prevent unauthorized access to data
Types of Views

• User-defined views:
• Views (sometimes called standard views)
• Indexed views
• Partitioned views

• System views:
• System catalog views
• Dynamic management views (DMVs)
• Compatibility views
• Information schema views
Advantages of Views

• Views have many advantages:


• Create a simplified view of the underlying table
relationships
• Provide data security—users see only what they need
to see
• Can create an interface between underlying data
structures and external applications
• If changes are made to tables, you need to make sure the
views still work
• Simplify reporting by providing data in the correct
format
Dynamic Management Views

• DMVs return internal SQL Server state


information
• DMFs take parameters
• DMVs and DMFs are together known as DMOs
• Use DMOs to:
• Monitor the health of a SQL Server instance
• Diagnose problems
• Tune performance
Other System Views

• Catalog views:
• Views onto internal system metadata
• Organized into categories, such as object views,
schema views, or linked server views
• Compatibility views:
• Provide backward compatibility for SQL Server 2000
system tables
• Do not use for new development work

• Information schema views:


• Comply with the ISO standard
Demonstration: Querying Catalog Views and
DMVs

In this demonstration, you will see how to:


• Query catalog views and
INFORMATION_SCHEMA views
• Query DMVs
Lesson 2: Creating and Managing Views

Create a View
Drop a View
Alter a View
Ownership Chains and Views
Sources of Information about Views
Updateable Views
Hide View Definitions
• Demonstration: Creating, Altering, and Dropping a
View
Create a View

• Use the CREATE VIEW statement to create a new


view
• View attributes
• WITH ENCRYPTION
• WITH SCHEMABINDING
• WITH VIEW_METADATA

• WITH CHECK option


• Ensures new records conform to the view definition
Drop a View

• To remove a view from the database, use the


DROP VIEW statement
• Also drops associated permissions
• Multiple views can be dropped in a single
statement
• Comma-delimited list of views to be deleted
Alter a View

• To alter an existing view definition, use the ALTER


VIEW statement
• Does not alter associated permissions
Ownership Chains and Views

• Views and tables that have the same owner:


• Another user can be given access to the view—even if
they don’t have permissions on the table
• This enables views to filter information from the
underlying table
• If a view and underlying tables have different
owners:
• Another user will not have access to the view
• Applies even if the owner of the view has access to the
tables
Sources of Information about Views

• Use SSMS to list all views in a database


• List columns, triggers, indexes, and statistics
• Script View As to create scripts for existing views

• Use Transact-SQL:
• sys.views – lists views in database
• OBJECT_DEFINITION() – returns the definition of non-
encrypted views
• sys.sql_expression_dependencies – lists objects,
including other views, that depend on an object
Updateable Views

• Data can be modified through a view, providing


that:
• The view includes columns from only one table
• The columns directly reference the table columns
• No aggregations or computations
• The updates comply with the base table constraints
• NULL or NOT NULL
• Primary and foreign keys can be enforced
• WITH CHECK option prevents data being inserted that
does not comply with the view definition
Hide View Definitions

• Use the WITH ENCRYPTION option to obfuscate


the view definition
• Limited protection
• Do not rely on WITH ENCRYPTION to protect the
view definition
Demonstration: Creating, Altering, and Dropping
a View

In this demonstration, you will see how to:


• Create a view using two AdventureWorksLT
tables
• Query the view and order the result set
• Alter the view to add the encryption option
• Drop the view
Lesson 3: Performance Considerations for Views

Views and Dynamic Resolution


Indexed Views
Nested View Considerations
• Partitioned Views
Views and Dynamic Resolution

• Dynamic resolution in view optimization can


assist performance
• SQL Server will not retrieve data it does not need
• Single query plan for both query and view
• Avoid using SELECT * in views
Indexed Views

• An indexed view is materialized and data is


stored on disk
• Nonindexed views store only the view definition
• An indexed view is not a table
• Two ways indexed views are used:
• Directly in a query; in some situations, the indexed view
will be faster
• Indirectly by the query optimizer; when it is beneficial
to use the indexed view instead of the underlying
tables
Nested View Considerations

• A nested view is one that calls another view—


that view may call another view, and so on
• Disadvantages include:
• Broken ownership chains
• Poorly performing queries that are difficult to debug
• Problems maintaining tangled code

• Advantages include:
• Once a view has been written, tested, and documented,
it can be used just like a table
Partitioned Views

• A partitioned view is a view onto a partitioned


table
• A partitioned table is a large table that has been
divided horizontally
• All tables have the same columns and data types
• Use a WITH CHECK constraint
• Update the underlying tables through the view
• A partitioned view may be local or distributed
• Performance benefits
• Faster querying
• Faster indexing
Lab: Designing and Implementing Views

Exercise 1: Creating Standard Views


• Exercise 2: Creating an Updateable View

Logon Information
Virtual machine: 20762C-MIA-SQL
User name: AdventureWorks\Student
Password: Pa55w.rd

Estimated Time: 45 minutes


Lab Scenario

A new web-based stock promotion is being tested


at the Adventure Works Bicycle Company. Your
manager is worried that providing access from the
web-based system directly to the database tables
will be insecure, so has asked you to design some
views for the web-based system.
The Sales department has also asked you to create
a view that enables a temporary worker to enter
new customer data without viewing credit card,
email address, or phone number information.
Lab Review

What are three requirements for a view to be


updateable?
• What is a standard, nonindexed view?
Module Review and Takeaways

• Review Question(s)

You might also like