Download Lesson06 Implementing Views

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Entity–attribute–value model wikipedia , lookup

Relational model wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Database model wikipedia , lookup

Object-relational impedance mismatch wikipedia , lookup

Transcript
Implementing Views
Advanced Database
Dr. AlaaEddin Almabhouh
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)
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
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)
What Is a View?
Employee (table)
EmployeeID
LastName
FirstName
Title
…
287
Mensa-Annan
Tete
Mr.
…
288
Abbas
Syed
Mr.
…
289
Valdez
Rachel
NULL
…
vEmployee (view)
LastName
FirstName
Mensa-Annan
Tete
Abbas
Syed
Valdez
Rachel
Types of Views

Standard views


Indexed views


Combine data from one or more base tables (or
views) into a new virtual table
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
Syntax for Creating Views

Use CREATE VIEW statement:
CREATE VIEW [ schema_name.] view_name [ (column [ ,...n ] ) ]
[WITH [ENCRYPTION] [SCHEMABINDING] [VIEW_METADATA] ]
AS select_statement [ ; ]
[ WITH CHECK OPTION ]

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
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’
Syntax for Altering and Dropping Views

Alter by using the ALTER VIEW Transact-SQL
statement:
ALTER VIEW [ schema_name.]view_name [ (column [ ,...n ] ) ]
[WITH [ENCRYPTION] [SCHEMABINDING] [VIEW_METADATA] ]
AS select_statement [ ; ]
[ WITH CHECK OPTION ]

Drop by using the DROP VIEW Transact-SQL
statement:
DROP VIEW [ schema_name.]view_name [ ...,n ] [ ; ]
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.[EmailAddress]
FROM [HumanResources].[Employee] e
INNER JOIN [Person].[Contact] c
ON c.[ContactID] = e.[ContactID]
Use WITH ENCRYPTION on ALTER VIEW statements to retain
encryption
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
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
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)
What Is a Partitioned View?

Joins horizontally partitioned data from a set of tables across
one or more servers
SQLServerNorth.Sales.Sale
vSales
SQLServerSouth.Sales.Sale
CREATE VIEW vSales AS
SELECT *
FROM SQLServerNorth.Sales.Sale
UNION ALL
SELECT *
FROM SQLServerSouth.Sales.Sale
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)
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)
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)
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)
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)
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)
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
Disadvantages of Views



Update restriction
Structure restriction
Performance
Slide 60 (of 82)
Q &A
Slide 81 (of 82)