Module 8: Implementing Views. Overview Introduction Advantages Definition Modifying Data Through Views Optimizing Performance by Using Views.

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Advertisements

Basic SQL Introduction Presented by: Madhuri Bhogadi.
What Is a User-defined Function? Scalar Functions –Similar to a built-in function Multi-Statement Table-valued Functions –Content like a stored procedure.
Module 6: Working with Subqueries. Overview Introduction to Subqueries Using a Subquery as a Derived Table Using a Subquery as an Expression Using a Subquery.
Index Blocking Factors, Views Rose-Hulman Institute of Technology Curt Clifton.
Module 5: Implementing Data Integrity. Overview Types of Data Integrity Enforcing Data Integrity Defining Constraints Types of Constraints Disabling Constraints.
Module 10: Implementing User-defined Functions. Overview What Is a User-defined Function? Defining Examples.
Module 9: Implementing Stored Procedures. Introduction to Stored Procedures Creating Executing Modifying Dropping Using Parameters in Stored Procedures.
Module 11: Implementing Triggers. Overview Introduction Defining Create, drop, alter triggers How Triggers Work Examples Performance Considerations Analyze.
Module 7: Creating and Maintaining Indexes. Overview Creating Indexes Creating Index Options Maintaining Indexes Introduction to Statistics Querying the.
Database Systems More SQL Database Design -- More SQL1.
Module 9: Managing Schema Objects. Overview Naming guidelines for identifiers in schema object definitions Storage and structure of schema objects Implementing.
Database Design for DNN Developers Sebastian Leupold.
Module 3: Managing Database Files. Overview Introduction to Data Structures Creating Databases Managing Databases Placing Database Files and Logs Optimizing.
Views: Limiting Access to Data A view is a named select statement that is stored in a database as an object. It allows you to view a subset of rows or.
Database Management. ICT5 Database Administration (DBA) The DBA’s tasks will include the following: 1. The design of the database. After the initial design,
Defining Stored Procedures Named Collections of Transact-SQL Statements Encapsulate Repetitive Tasks Five Types (System, Local, Temporary, Remote, and.
Module 8: Implementing Stored Procedures. Introducing Stored Procedures Creating, Modifying, Dropping, and Executing Stored Procedures Using Parameters.
Module 1: Introduction to Transact-SQL
Module 9 Designing and Implementing Stored Procedures.
Module 9: Introduction to Programming Objects. Overview Displaying the Text of a Programming Object Introduction to Views Advantages of Views Creating.
Module 4: Managing Security. Overview Implementing an Authentication Mode Assigning Login Accounts to Users and Roles Assigning Permissions to Users and.
Module 11: Programming Across Multiple Servers. Overview Introducing Distributed Queries Setting Up a Linked Server Environment Working with Linked Servers.
Chapter 6 Additional Database Objects Oracle 10g: SQL.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
Views Lesson 7.
Module 4 Designing and Implementing Views. Module Overview Introduction to Views Creating and Managing Views Performance Considerations for Views.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
Module 4: Implementing Data Integrity
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
Permissions Lesson 13. Skills Matrix Security Modes Maintaining data integrity involves creating users, controlling their access and limiting their ability.
Indexes and Views Unit 7.
Module 7: Modifying Data. Overview Using Transactions Inserting Data Deleting Data Updating Data Performance Considerations.
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.
Working with SQL Server Database Objects Faculty: Nguyen Ngoc Tu.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Session 1 Module 1: Introduction to Data Integrity
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 5 SQL.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
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 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.
Module 6: Creating and Maintaining Indexes. Overview Creating Indexes Understanding Index Creation Options Maintaining Indexes Introducing Statistics.
In this session, you will learn to: Create and manage views Implement a full-text search Implement batches Objectives.
Module 9: Implementing Functions. Overview Creating and Using Functions Working with Functions Controlling Execution Context.
Module 9: Implementing User-Defined Functions. Overview Introducing User-Defined Functions Implementing User-Defined Functions.
Views / Session 3/ 1 of 40 Session 3 Module 5: Implementing Views Module 6: Managing Views.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Table General Guidelines for Better System Performance
Implementing Views Advanced Database Dr. AlaaEddin Almabhouh.
Module 10: Implementing Triggers
STORED PROCEDURES AND FUNCTION (9.6.1)
Module 7: Implementing Views
Module 5: Implementing Data Integrity by Using Constraints
Chapter 2 Views.
Table General Guidelines for Better System Performance
Chapter 2 Views.
Introduction To Structured Query Language (SQL)
Contents Preface I Introduction Lesson Objectives I-2
Chapter 8 Advanced SQL.
Database Management System
IST 318 Database Administration
Presentation transcript:

Module 8: Implementing Views

Overview Introduction Advantages Definition Modifying Data Through Views Optimizing Performance by Using Views

Introduction to Views EmployeeViewEmployeeView Lastname Firstname Davolio Fuller Leverling Davolio Fuller Leverling Nancy Andrew Janet Nancy Andrew Janet EmployeesEmployees EmployeeID LastName Firstname Title Davolio Fuller Leverling Davolio Fuller Leverling Nancy Andrew Janet Nancy Andrew Janet ~~~ User’s View USE Northwind GO CREATE VIEW dbo.EmployeeView AS SELECT LastName, Firstname FROM Employees USE Northwind GO CREATE VIEW dbo.EmployeeView AS SELECT LastName, Firstname FROM Employees

Advantages of Views Focus the Data for Users Focus on important or appropriate data only Limit access to sensitive data (hide SSN from professors) Mask Database Complexity Hide complex database design Simplify complex queries, including distributed queries to heterogeneous data by embedding them in views Simplify Management of User Permissions Different user access DB from different views Improve Performance Reduce data access Organize Data for Export to Other Applications

 Defining Views Creating Views Example: View of Joined Tables Altering and Dropping Views Avoiding Broken Ownership Chains Locating View Definition Information Hiding View Definitions

Creating Views Creating a View Restrictions on View Definitions Cannot include ORDER BY clause Cannot include INTO keyword CREATE VIEW dbo.OrderSubtotalsView (OrderID, Subtotal) AS SELECT OD.OrderID, SUM(CONVERT(money,(OD.UnitPrice*Quantity*(1-Discount)/100))*100) FROM [Order Details] OD GROUP BY OD.OrderID GO CREATE VIEW dbo.OrderSubtotalsView (OrderID, Subtotal) AS SELECT OD.OrderID, SUM(CONVERT(money,(OD.UnitPrice*Quantity*(1-Discount)/100))*100) FROM [Order Details] OD GROUP BY OD.OrderID GO

Example: View of Joined TablesOrderIDOrderID CustomerIDCustomerID BONAP PICCO QUICK BONAP PICCO QUICK ~~~ RequiredDateRequiredDate ShippedDateShippedDate OrdersCustomers ShipStatusView USE Northwind GO CREATE VIEW dbo.ShipStatusView AS SELECT OrderID, ShippedDate, ContactName FROM Customers C INNER JOIN Orders O ON C.CustomerID = O.CustomerID WHERE RequiredDate < ShippedDate USE Northwind GO CREATE VIEW dbo.ShipStatusView AS SELECT OrderID, ShippedDate, ContactName FROM Customers C INNER JOIN Orders O ON C.CustomerID = O.CustomerID WHERE RequiredDate < ShippedDateCustomerIDCustomerID BONAP PICCO QUICK BONAP PICCO QUICKCompanyNameCompanyName Bon app' Piccolo und mehr QUICK-Stop Bon app' Piccolo und mehr QUICK-StopContactNameContactName Laurence Lebihan Georg Pipps Horst Kloss Laurence Lebihan Georg Pipps Horst Kloss OrderIDOrderID ShippedDateShippedDate ContactNameContactName Laurence Lebihan Georg Pipps Horst Kloss Laurence Lebihan Georg Pipps Horst Kloss

Altering and Dropping Views Altering Views Retains assigned permissions Causes new SELECT statement and options to replace existing definition Dropping Views USE Northwind GO ALTER VIEW dbo.EmployeeView AS SELECT LastName, FirstName, Extension FROM Employees USE Northwind GO ALTER VIEW dbo.EmployeeView AS SELECT LastName, FirstName, Extension FROM Employees DROP VIEW dbo.ShipStatusView

GRANT SELECT ON view2 TO azhar SELECT * FROM reif.view2 Dependent Objects with Different Owners Example: Reif executes: Azhar executes: Avoiding Broken Ownership Chains reif.view2 lewis.view1 lewis.table1 azhar does not have rights to lewis.view1 and lewis.table1 azhar does not have rights to lewis.view1 and lewis.table1

Locating View Definition Information Locating View Definitions Not available if view was created using WITH ENCRYPTION option sysobjects: view name sysdepends: base object names syscomments: view definition syscolumns: columns in the view sp_helptext: provide text used in view definition Locating View Dependencies sp_depends objectname Lists objects upon which view depends Lists objects that depend on a view

Hiding View Definitions Use the WITH ENCRYPTION Option Do Not Delete Entries in the syscomments Table USE Northwind GO CREATE VIEW dbo.[Order Subtotals] WITH ENCRYPTION AS SELECT OrderID, Sum(CONVERT(money,(UnitPrice*Quantity*(1-Discount)/100))*100) AS Subtotal FROM [Order Details] GROUP BY OrderID GO USE Northwind GO CREATE VIEW dbo.[Order Subtotals] WITH ENCRYPTION AS SELECT OrderID, Sum(CONVERT(money,(UnitPrice*Quantity*(1-Discount)/100))*100) AS Subtotal FROM [Order Details] GROUP BY OrderID GO

Modifying Data Through Views Can Affect Only One Underlying Table Cannot Be Made to Certain Columns (such as computed columns) Can Cause Errors If They Affect Columns That Are Not Referenced in the View Are Verified If the WITH CHECK OPTION Has Been Specified

 Optimizing Performance By Using Views Performance Considerations Using Indexed Views Using Views to Partition Data

Performance Considerations USE Northwind GO CREATE VIEW dbo.TopSalesView AS SELECT * FROM dbo.TotalPurchaseView WHERE Subtotal > GO USE Northwind GO CREATE VIEW dbo.TopSalesView AS SELECT * FROM dbo.TotalPurchaseView WHERE Subtotal > GO TotalPurchaseView 1 1 ~ ~ ~ ~ ~ ~ ~ ~ 2 2 ~ ~ ~ ~ ~ ~ ~ ~ 3 3 ~ ~ ~ ~ ~ ~ ~ ~ 4 4 ~ ~ ~ ~ ~ ~ ~ ~ 5 5 ~ ~ ~ ~ ~ ~ ~ ~ 6 6 ~ ~ ~ ~ ~ ~ ~ ~ CustomersCustomers 1 1 ~ ~ ~ ~ ~ ~ n n 2 2 ~ ~ ~ ~ ~ ~ n n 3 3 ~ ~ ~ ~ ~ ~ y y 4 4 ~ ~ ~ ~ ~ ~ y y 5 5 ~ ~ ~ ~ ~ ~ n n 6 6 ~ ~ ~ ~ ~ ~ y y OrdersOrders 1 1 ~ ~ ~ ~ ~ ~ n n 2 2 ~ ~ ~ ~ ~ ~ n n 3 3 ~ ~ ~ ~ ~ ~ y y 4 4 ~ ~ ~ ~ ~ ~ y y 5 5 ~ ~ ~ ~ ~ ~ n n 6 6 ~ ~ ~ ~ ~ ~ y y Order Details 1 1 ~ ~ ~ ~ ~ ~ ~ ~ 2 2 ~ ~ ~ ~ ~ ~ ~ ~ 3 3 ~ ~ ~ ~ ~ ~ ~ ~ 4 4 ~ ~ ~ ~ ~ ~ ~ ~ 5 5 ~ ~ ~ ~ ~ ~ ~ ~ 6 6 ~ ~ ~ ~ ~ ~ ~ ~ SELECT * FROM dbo.TopSalesView WHERE CompanyName = 'Ernst Handel' SELECT * FROM dbo.TopSalesView WHERE CompanyName = 'Ernst Handel' TopSalesViewTopSalesView ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ TopSalesView depends on TotalPurchaseView: Any performance problems in the underlying view can be hidden.

Using Indexed Views Indexed Views Store the Result Sets in the Database Creating an Indexed View Guidelines for Creating Indexed Views Note: Query optimizer can determine using the access statistic.. let it do that. Use explicitly when: Performance gains outweigh maintenance costs Underlying data is infrequently updated Queries perform many joins and aggregations

Using Indexed Views Restrictions on Creating Indexed Views First index must be UNIQUE CLUSTERED Create view with SCHEMABINDING option View cannot reference other views

Using Views to Partition Data You Can Use Views to Partition Data Across Multiple Servers or Instances of SQL Server Use UNION to combine multiple tables across servers How SQL Server Uses Views to Partition Data Tables can be partitioned on separate servers (or processors) for parallel scanning How Partitioned Views Improve Performance Parallel processing

Recommended Practices dbo Should Own All Views Verify Object Dependencies Before You Drop Objects Carefully Evaluate Creating Views Based on Views Never Delete Entries in the syscomments Table Use a Standard Naming Convention

Review Introduction to Views Advantages of Views Defining Views Modifying Data Through Views Optimizing Performance by Using Views