Module 4 Designing and Implementing Views. Module Overview Introduction to Views Creating and Managing Views Performance Considerations for Views.

Slides:



Advertisements
Similar presentations
Module 3: Creating and Tuning Indexes. Planning Indexes Creating Indexes Optimizing Indexes.
Advertisements

February 18, 2012 Lesson 3 Standard SQL. Lesson 3 Standard SQL.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Module 8 Importing and Exporting Data. Module Overview Transferring Data To/From SQL Server Importing & Exporting Table Data Inserting Data in Bulk.
Module 12: Auditing SQL Server Environments
Module 17 Tracing Access to SQL Server 2008 R2. Module Overview Capturing Activity using SQL Server Profiler Improving Performance with the Database Engine.
Module 2 Designing a Logical Database Model. Module Overview Guidelines for Building a Logical Database Model Planning for OLTP Activity Evaluating Logical.
Module 8: Implementing Views. Overview Introduction Advantages Definition Modifying Data Through Views Optimizing Performance by Using Views.
Database Systems More SQL Database Design -- More SQL1.
Module 9 Designing an XML Strategy. Module 9: Designing an XML Strategy Designing XML Storage Designing a Data Conversion Strategy Designing an XML Query.
Database Design for DNN Developers Sebastian Leupold.
Module 12 Handling Errors in T-SQL Code. Module Overview Understanding T-SQL Error Handling Implementing T-SQL Error Handling Implementing Structured.
Implementing File and Print Services
Module 8 Improving Performance through Nonclustered Indexes.
Module 19 Managing Multiple Servers. Module Overview Working with Multiple Servers Virtualizing SQL Server Deploying and Upgrading Data-Tier Applications.
Module 7 Reading SQL Server® 2008 R2 Execution Plans.
Module 9 Designing and Implementing Stored Procedures.
Module 11: Programming Across Multiple Servers. Overview Introducing Distributed Queries Setting Up a Linked Server Environment Working with Linked Servers.
11 Copyright © 2007, Oracle. All rights reserved. Creating Other Schema Objects.
Module 5 Planning for SQL Server® 2008 R2 Indexing.
Module 14 Configuring Security for SQL Server Agent.
11 Copyright © Oracle Corporation, All rights reserved. Creating Views.
Views Lesson 7.
Module 3 Configuring File Access and Printers on Windows 7 Clients.
Module 3 Designing and Implementing Tables. Module Overview Designing Tables Working with Schemas Creating and Altering Tables.
Indexes and Views Unit 7.
Module 11 Authorizing Users to Access Resources. Module Overview Authorizing User Access to Objects Authorizing Users to Execute Code Configuring Permissions.
Module 7: Implementing Views. Overview Introducing Views Defining and Using Views Using Views to Optimize Performance.
Chapter 13 Views Oracle 10g: SQL. Oracle 10g: SQL2 Objectives Create a view, using CREATE VIEW command or the CREATE OR REPLACE VIEW command Employ the.
SQL/Lesson 7/Slide 1 of 32 Implementing Indexes Objectives In this lesson, you will learn to: * Create a clustered index * Create a nonclustered index.
Working with SQL Server Database Objects Faculty: Nguyen Ngoc Tu.
Module 5: Implementing Merge Replication. Overview Understanding Merge Replication Architecture Implementing Conflict Resolution Planning and Deploying.
Module 1 Introduction to SQL Server® 2008 R2 and its Toolset.
Session 1 Module 1: Introduction to Data Integrity
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL.
Learningcomputer.com SQL Server 2008 –Views, Functions and Stored Procedures.
SQL Server 2012 Session: 1 Session: 12 Triggers Data Management Using Microsoft SQL Server.
Stored Procedures / Session 4/ 1 of 41 Session 4 Module 7: Introducing stored procedures Module 8: More about stored procedures.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
Module 10 Merging Data and Passing Tables. Module Overview Using the MERGE Statement Implementing Table Types Using Table Types As Parameters.
Module 9: Using Advanced Techniques. Considerations for Querying Data Working with Data Types Cursors and Set-Based Queries Dynamic SQL Maintaining Query.
Module 8: Using Programming Objects for Data Retrieval.
Retele de senzori Curs 2 - 1st edition UNIVERSITATEA „ TRANSILVANIA ” DIN BRAŞOV FACULTATEA DE INGINERIE ELECTRICĂ ŞI ŞTIINŢA CALCULATOARELOR.
SQL Triggers, Functions & Stored Procedures Programming Operations.
SQL Basics Review Reviewing what we’ve learned so far…….
Module 9: Implementing Functions. Overview Creating and Using Functions Working with Functions Controlling Execution Context.
Views / Session 3/ 1 of 40 Session 3 Module 5: Implementing Views Module 6: Managing Views.
Module 5: Working with Subqueries. Writing Basic Subqueries Writing Correlated Subqueries Comparing Subqueries with Joins and Temporary Tables Using Common.
2 Copyright © 2008, Oracle. All rights reserved. Building the Physical Layer of a Repository.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Microsoft SQL Server 2014 for Oracle DBAs Module 8
Implementing Views Advanced Database Dr. AlaaEddin Almabhouh.
Quiz Questions Q.1 An entity set that does not have sufficient attributes to form a primary key is a (A) strong entity set. (B) weak entity set. (C) simple.
Module 4: Creating and Tuning Indexes
Creating Views Schedule: Timing Topic 20 minutes Lecture
20761A 10: Using Subqueries Module 10   Using Subqueries.
Module 7: Implementing Views
Module 5: Implementing Data Integrity by Using Constraints
Overview Implementing Triggers Implementing XML Schemas.
Chapter 4 Indexes.
CH 4 Indexes.
Chapter 2 Views.
Using Table Expressions
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Microsoft SQL Server 2014 for Oracle DBAs Module 7
CH 4 Indexes.
Chapter 2 Views.
Contents Preface I Introduction Lesson Objectives I-2
Responding to Data Manipulation Via Triggers
Presentation transcript:

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 Demonstration 1A: System and Dynamic Management Views

What is a View? Employee (table) EmployeeIDLastNameFirstNameTitleBirthDate… 287Mensa-AnnanTeteMr.3/2/1984… 288AbbasSyedMr.4/5/1976… 289ValdezRachelNULL9/8/1973… EmployeeList (view) EmployeeIDLastNameFirstName 287Mensa-AnnanTete 288AbbasSyed 289ValdezRachel A view is a database object referenced like a table A view is like a SELECT query with a name A view is a database object referenced like a table A view is like a SELECT query with a name

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

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

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.

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_*

Demonstration 1A: System and Dynamic Management Views In this demonstration, you will see how to: Query system views Query dynamic management 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

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.EmployeeList AS SELECT EmployeeID, LastName, FirstName FROM HumanResources.Employee; CREATE VIEW HumanResources.EmployeeList AS SELECT EmployeeID, LastName, FirstName FROM HumanResources.Employee;

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;

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.EmployeeList AS SELECT EmployeeID, LastName, FirstName, PostalCode FROM HumanResources.Employee; ALTER VIEW HumanResources.EmployeeList AS SELECT EmployeeID, LastName, FirstName, PostalCode FROM HumanResources.Employee;

Ownership Chains and Views

Sources of Information About Views SQL Server Management Studio Transact-SQL SourceInformation Object Explorer List of views in database Access to columns, triggers, indexes, and statistics defined on views View Properties dialog box Properties of individual views SourceInformation sys.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

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

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.EmployeeList WITH ENCRYPTION AS SELECT EmployeeID, LastName, FirstName FROM HumanResources.Employee; CREATE VIEW HumanResources.EmployeeList WITH ENCRYPTION AS SELECT EmployeeID, LastName, FirstName FROM HumanResources.Employee; Use WITH ENCRYPTION on ALTER VIEW to retain encryption

Demonstration 2A: Implementing Views In 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

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

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

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

Partitioned Views vSales SQLServerSouth.Sales.Sale SQLServerNorth.Sales.Sale CREATE VIEW Sales.AllSales AS SELECT * FROM SQLServerNorth.Sales.Sale UNION ALL SELECT * FROM SQLServerSouth.Sales.Sale; CREATE VIEW Sales.AllSales AS SELECT * FROM SQLServerNorth.Sales.Sale UNION ALL SELECT * FROM SQLServerSouth.Sales.Sale; Joins partitioned data from set of tables across servers

Demonstration 3A: Views and Performance In this demonstration, you will see how: Views are eliminated in query plans Views are expanded and integrated into the outer query before being optimized

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

Lab Scenario A 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.

Lab Review What considerations are there for views that involve multiple tables? What is required for columns in views that are created from expressions?

Module Review and Takeaways Review Questions Best Practices