27
Module 4 Designing and Implementing Views

Module 4 Designing and Implementing Views

  • Upload
    caia

  • View
    75

  • Download
    0

Embed Size (px)

DESCRIPTION

Module 4 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 Working with System Views Dynamic Management Views - PowerPoint PPT Presentation

Citation preview

Page 1: Module 4 Designing and Implementing Views

Module 4Designing and

Implementing Views

Page 2: Module 4 Designing and Implementing Views

Module Overview• Introduction to Views• Creating and Managing Views• Performance Considerations for Views

Page 3: Module 4 Designing and Implementing Views

Lesson 1: Introduction to Views• What is a View?• Types of Views• Advantages of Views• Working with System Views• Dynamic Management Views• Demonstration 1A: System and Dynamic Management

Views

Page 4: Module 4 Designing and Implementing Views

What is a View?

Employee (table)

EmployeeID LastName FirstName Title BirthDate …

287 Mensa-Annan Tete Mr. 3/2/1984 …

288 Abbas Syed Mr. 4/5/1976 …

289 Valdez Rachel NULL 9/8/1973 …

EmployeeList (view)EmployeeID LastName FirstName

287 Mensa-Annan Tete

288 Abbas Syed

289 Valdez Rachel

• A view is a database object referenced like a table• A view is like a SELECT query with a name

Page 5: Module 4 Designing and Implementing Views

Types of Views

• Standard views Combine data from one or more base tables into a new virtual

table• System Views

Provide state information for SQL Server and catalogs• Indexed views

View that has been computed and stored. You index a view by creating a unique clustered index on it

• Partitioned views Joins horizontally partitioned data from a set of tables across one

or more servers

Page 6: Module 4 Designing and Implementing Views

Advantages of Views

• Mask database complexity

• Simplify management of user permissions

• Organize data for export to other applications

• Focus the data for a user

• Provide backward compatibility

• Structure data for reporting applications

Page 7: Module 4 Designing and Implementing Views

Working with System Views

There are several types of system views:• Catalog Views

Return information used by the SQL Server Database Engine

• Compatibility Views Many system tables available in earlier versions are now

available as system views• Information Schema Views

Provide catalog information as defined in the ISO standard for the INFORMATION_SCHEMA.

Page 8: Module 4 Designing and Implementing Views

Dynamic Management Views

• Monitor server health

• Diagnose problems, tune performance

• Some are implemented as functions, some as views

• Return server state information

• Named like sys.dm_*

Page 9: Module 4 Designing and Implementing Views

Demonstration 1A: System and Dynamic Management ViewsIn this demonstration, you will see how to:• Query system views• Query dynamic management views

Page 10: Module 4 Designing and Implementing Views

Lesson 2: Creating and Managing Views• Creating Views• Dropping Views• Altering Views• Ownership Chains and Views• Sources of Information about Views• Updatable Views• Obfuscating Views Definitions• Demonstration 2A: Implementing Views

Page 11: Module 4 Designing and Implementing Views

Creating Views

• Use the CREATE VIEW Transact-SQL statement:

• Restrictions on creating views: Cannot nest more than 32 levels deep Cannot use ORDER BY without TOP

CREATE VIEW HumanResources.EmployeeListAS SELECT EmployeeID, LastName, FirstNameFROM HumanResources.Employee;

Page 12: Module 4 Designing and Implementing Views

Dropping Views

• Drop by using the DROP VIEW Transact-SQL statement:

• Also drops all permissions associated with the view• Multiple views can be dropped in a single statement

Comma-delimited list of views

DROP VIEW HumanResources.EmployeeList;

Page 13: Module 4 Designing and Implementing Views

Altering Views

• Alter by using the ALTER VIEW Transact-SQL statement:

• Replaces the definition of a view• Does not alter permissions associated with the view

ALTER VIEW HumanResources.EmployeeListAS SELECT EmployeeID, LastName, FirstName, PostalCodeFROM HumanResources.Employee;

Page 14: Module 4 Designing and Implementing Views

Ownership Chains and Views

Page 15: Module 4 Designing and Implementing Views

Sources of Information About Views

SQL Server Management Studio

Transact-SQL

Source Information

Object ExplorerList of views in databaseAccess to columns, triggers, indexes, and statistics defined on views

View Properties dialog box Properties of individual views

Source Informationsys.views List of views in database

OBJECT_DEFINITION() Function that returns definition of non-encrypted views

sys.sql_expression_dependencies Objects (including views) that depend on other objects

Page 16: Module 4 Designing and Implementing Views

Updatable Views

• Views do not maintain a separate copy of data• Updates to views modify the base tables• Updates are restricted by using the WITH CHECK OPTION

• Restrictions:• Cannot affect more than one base table• Cannot modify columns derived from functions or expressions• Cannot modify columns affected by GROUP BY, HAVING, or

DISTINCT clauses

Page 17: Module 4 Designing and Implementing Views

Obfuscating View Definitions

• WITH ENCRYPTION clause• Encrypts view definition stored in SQL Server• Protects view creation logic to a limited extent• Generally not recommended

CREATE VIEW HumanResources.EmployeeListWITH ENCRYPTIONAS SELECT EmployeeID, LastName, FirstNameFROM HumanResources.Employee;

Use WITH ENCRYPTION on ALTER VIEW to retain encryption

Page 18: Module 4 Designing and Implementing Views

Demonstration 2A: Implementing ViewsIn this demonstration, you will see how to:• Create a view• Query a view• Query the definition of a view• Use the WITH ENCRYPTION option• Drop a view• Generate a script for an existing view

Page 19: Module 4 Designing and Implementing Views

Lesson 3: Performance Considerations for Views• Views and Dynamic Resolution• Nested View Considerations• Partitioned Views• Demonstration 3A: Views and Performance

Page 20: Module 4 Designing and Implementing Views

Views and Dynamic Resolution

• Dynamic resolution in view optimization can assist performance

• SQL Server does not retrieve data that it doesn’t need

• Single query plan for both query and view

• Query plans do not show standard views

• SELECT * needs special consideration in views Should be avoided

Page 21: Module 4 Designing and Implementing Views

Nested View Considerations

• Views can provide a convenient layer of abstraction in coding

• Views can be nested up to 32 levels

• Nested views can hide code complexity More difficult to troubleshoot performance issues More difficult to understand code complexity

Page 22: Module 4 Designing and Implementing Views

Partitioned Views

vSales

SQLServerSouth.Sales.SaleSQLServerNorth.Sales.Sale

CREATE VIEW Sales.AllSales AS SELECT * FROM SQLServerNorth.Sales.SaleUNION ALLSELECT * FROM SQLServerSouth.Sales.Sale;

Joins partitioned data from set of tables across servers

Page 23: Module 4 Designing and Implementing Views

Demonstration 3A: Views and PerformanceIn this demonstration, you will see how:• Views are eliminated in query plans• Views are expanded and integrated into the outer query

before being optimized

Page 24: Module 4 Designing and Implementing Views

Lab 4: Designing and Implementing Views • Exercise 1: Design, Implement and Test the WebStock

Views• Exercise 2: Design and Implement the Contacts View• Challenge Exercise 3: Modify the AvailableModels View

(only if time permits)

Logon information

Estimated time: 45 minutes

Virtual machine 623XB-MIA-SQLUser name AdventureWorks\AdministratorPassword Pa$$w0rd

Page 25: Module 4 Designing and Implementing Views

Lab ScenarioA new web-based stock promotion system is being rolled out. Your manager is very concerned about providing access from the web-based system directly to the tables in your database. She has requested you to design some views that the web-based system could connect to instead.Details of organizational contacts are held in a number of tables. The relationship management system being used by the account management team needs to be able to gain access to these contacts. However, they need a single view that comprises all contacts. You need to design, implement and test the required view.Finally, if you have time, a request has been received from the new Marketing team that the catalog description of the product models should be added to the AvailableModels view. They would appreciate you modifying the view to provide this additional column.

Page 26: Module 4 Designing and Implementing Views

Lab Review• What considerations are there for views that involve

multiple tables?• What is required for columns in views that are created

from expressions?

Page 27: Module 4 Designing and Implementing Views

Module Review and Takeaways• Review Questions• Best Practices