Presentation is loading. Please wait.

Presentation is loading. Please wait.

Implementing Views Advanced Database Dr. AlaaEddin Almabhouh.

Similar presentations


Presentation on theme: "Implementing Views Advanced Database Dr. AlaaEddin Almabhouh."— Presentation transcript:

1 Implementing Views Advanced Database Dr. AlaaEddin Almabhouh

2 Topic & Structure of Lesson
What Is a View? Types of Views Advantages of Views Syntax for Creating Views Performance Considerations for Views Slide 2 (of 33)

3 What Is a View? Dynamic result of one or more tables operating on base relations to produce another relation. Virtual relation that does not necessarily actually exist in the database but is produced upon request, at time of request. The rows and columns of data come from tables referenced in the query defining the view and are produced dynamically when the view is referenced. Common examples of views are: A subset of rows or columns of a base table. A union of two or more base tables. A join of two or more base tables. A statistical summary of a base table. A subset of another view, or some combination of views and base table

4 What Is a View? Contents of a view are defined as a query on one or more base relations. With view resolution, any operations on view are automatically translated into operations on relations from which it is derived. With view materialization, the view is stored as a temporary table, which is maintained as the underlying base tables are updated. Slide 31 (of 82)

5 What Is a View? Employee (table) vEmployee (view) EmployeeID LastName
FirstName Title 287 Mensa-Annan Tete Mr. 288 Abbas Syed 289 Valdez Rachel NULL vEmployee (view) LastName FirstName Mensa-Annan Tete Abbas Syed Valdez Rachel

6 Types of Views Standard views Indexed views Partitioned views
Combine data from one or more base tables (or views) into a new virtual table Indexed views Materialize (persist) the view through the creation of a unique clustered index on the view Partitioned views Join horizontally partitioned data from one or more base tables across one or more servers

7 Syntax for Creating Views
Use CREATE VIEW statement: Restrictions: Cannot nest more than 32 levels deep Cannot contain more than 1,024 columns Cannot use COMPUTE, COMPUTE BY, or INTO Cannot use ORDER BY without TOP CREATE VIEW [ schema_name.] view_name [ (column [ ,...n ] ) ] [WITH [ENCRYPTION] [SCHEMABINDING] [VIEW_METADATA] ] AS select_statement [ ; ] [ WITH CHECK OPTION ]

8 Sample for Creating Views
CREATE VIEW Orders_View AS SELECT OrderID, OrderDate, o.CustomerID, CompanyName,Address, Phone FROM dbo.Orders o JOIN dbo.Customers c on o.CustomerID = c.CustomerID WHERE Country = ‘USA’

9 Syntax for Altering and Dropping Views
Alter by using the ALTER VIEW Transact-SQL statement: Drop by using the DROP VIEW Transact-SQL statement: ALTER VIEW [ schema_name.]view_name [ (column [ ,...n ] ) ] [WITH [ENCRYPTION] [SCHEMABINDING] [VIEW_METADATA] ] AS select_statement [ ; ] [ WITH CHECK OPTION ] DROP VIEW [ schema_name.]view_name [ ...,n ] [ ; ]

10 View Encryption Use the WITH ENCRYPTION option on CREATE VIEW Transact-SQL statement Encrypts view definition in sys.syscomments table Protects view creation logic CREATE VIEW [HumanResources].[vEmployee] WITH ENCRYPTION AS SELECT e.[EmployeeID],c.[Title],c.[FirstName],c.[MiddleName] ,c.[LastName],c.[Suffix],e.[Title] AS [JobTitle] ,c.[Phone],c.[ Address] FROM [HumanResources].[Employee] e INNER JOIN [Person].[Contact] c ON c.[ContactID] = e.[ContactID] Use WITH ENCRYPTION on ALTER VIEW statements to retain encryption

11 Considerations for Modifying Data in a View
Views do not maintain a separate copy of data Updates to views modify base tables Restrictions: Cannot affect more than one base table Cannot modify columns derived from aggregate functions or calculations Cannot modify columns affected by GROUP BY, HAVING, or DISTINCT clauses Updates to views are restricted by using the WITH CHECK OPTION

12 Performance Considerations for Views
Views introduce performance overhead because views are resolved dynamically Nested views introduce risk of performance problems Review definition of unencrypted nested views Use SQL Server Profiler to review performance Indexed views and partitioned views can improve performance

13 What Is an Indexed View? A view with a unique clustered index
Materializes view, improving performance Allows query optimizer to use view in query resolution Use when: Performance gains outweigh maintenance overhead Underlying data is modified infrequently Queries perform a significant number of joins and aggregations CREATE UNIQUE CLUSTERED INDEX [IX_vStateProvinceCountryRegion] ON [Person].[vStateProvinceCountryRegion] ( [StateProvinceID] ASC, [CountryRegionCode] ASC)

14 What Is a Partitioned View?
Joins horizontally partitioned data from a set of tables across one or more servers SQLServerNorth.Sales.Sale SQLServerSouth.Sales.Sale CREATE VIEW vSales AS SELECT * FROM SQLServerNorth.Sales.Sale UNION ALL SELECT * FROM SQLServerSouth.Sales.Sale vSales

15 Restrictions on Views SQL imposes several restrictions on creation and use of views. If column in view is based on an aggregate function: Column may appear only in SELECT and ORDER BY clauses of queries that access view. Column may not be used in WHERE nor be an argument to an aggregate function in any query based on view. Slide 44 (of 82)

16 Restrictions on Views For example, following query would fail:
SELECT COUNT(cnt) FROM StaffPropCnt; Similarly, following query would also fail: SELECT * FROM StaffPropCnt WHERE cnt > 2; Slide 45 (of 82)

17 Restrictions on Views Grouped view may never be joined with a base table or a view. For example, StaffPropCnt view is a grouped view, so any attempt to join this view with another table or view fails. Slide 46 (of 82)

18 View Updatability All updates to base table reflected in all views that encompass base table. Similarly, may expect that if view is updated then base table(s) will reflect change. Slide 47 (of 82)

19 View Updatability If change definition of view and replace count with actual property numbers: CREATE VIEW StaffPropList (branchNo, staffNo, propertyNo) AS SELECT s.branchNo, p.propertyNo FROM Staff s, PropertyForRent p WHERE s.staffNo = p.staffNo; Slide 49 (of 82)

20 View Updatability Now try to insert the record:
INSERT INTO StaffPropList VALUES (‘B003’, ‘PG19’); Still problem, because in PropertyForRent all columns except postcode/staffNo are not allowed nulls. However, have no way of giving remaining non-null columns values. Slide 50 (of 82)

21 Advantages of Views Focus the data for a user Mask database complexity
Simplify management of user permissions Improve performance Organize data for export to other applications

22 Disadvantages of Views
Update restriction Structure restriction Performance Slide 60 (of 82)

23 Q & A Slide 81 (of 82)


Download ppt "Implementing Views Advanced Database Dr. AlaaEddin Almabhouh."

Similar presentations


Ads by Google