Published on

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • CIS-282 Views 10/27/11
  • Views

    1. 1. Views <ul><li>Similar to an Access query </li></ul><ul><ul><li>Predefined SELECT statement </li></ul></ul><ul><ul><li>At most basic level specifies which columns and rows to return </li></ul></ul><ul><li>Provides a virtual table </li></ul><ul><ul><li>SELECT * FROM Customers represents rows and columns but isn’t for storing data </li></ul></ul>
    2. 2. Reasons for Views <ul><li>Reduce database complexity </li></ul><ul><li>‘ Hide’ sensitive data </li></ul><ul><li>Improve query performance </li></ul>
    3. 3. Reduce Complexity <ul><li>Provide access to data from multiple tables </li></ul><ul><ul><li>Normalization doesn’t get in the way </li></ul></ul><ul><li>Limit number of columns returned </li></ul><ul><ul><li>Instead of returning all columns just return ones commonly used </li></ul></ul>
    4. 4. Hiding Data <ul><li>Provide only columns that are appropriate </li></ul><ul><li>Provide only rows that are appropriate </li></ul><ul><li>Can have multiple views on one table with different fields and criteria for different user groups </li></ul><ul><li>Can also hide table and database organization </li></ul>
    5. 5. Ease of Use <ul><li>Can provide access to current information </li></ul><ul><ul><li>List only students who are currently enrolled in a course </li></ul></ul><ul><li>Can organize data for common reports </li></ul><ul><ul><li>List students with assignments for a section </li></ul></ul>
    6. 6. View Options <ul><li>Encryption prevents viewing the SQL statement </li></ul><ul><li>Schema binding ties the view to objects it depends on </li></ul><ul><ul><li>Can’t drop a table – or column – that’s referenced in a bound view </li></ul></ul><ul><li>Can sort results </li></ul><ul><ul><li>Requires use of TOP option </li></ul></ul><ul><ul><li>Can also use ORDER BY in SELECT where view is used </li></ul></ul>
    7. 7. Performance <ul><li>By default views run just as a query processed from command line </li></ul><ul><ul><li>Adds second step to execution: </li></ul></ul><ul><ul><ul><li>select from view </li></ul></ul></ul><ul><ul><ul><li>execute view itself </li></ul></ul></ul><ul><li>Can add an index to speed execution </li></ul>
    8. 8. Indexed Views <ul><li>Speeds up execution of query, but has rules: </li></ul><ul><ul><li>View must be schema-bound </li></ul></ul><ul><ul><li>View can’t reference other views (only tables & functions) </li></ul></ul><ul><ul><li>Two-part names required, and must be same owner as view (dbo.Students) </li></ul></ul>
    9. 9. Indexed Views (cont.) <ul><ul><li>View and source tables must be in same database </li></ul></ul><ul><ul><li>Functions used must be deterministic </li></ul></ul><ul><ul><li>ANSI_NULLS and QUOTED_IDENTIFIERS must be on when view and source tables created </li></ul></ul><ul><ul><ul><li>ANSI_NULL – Set to on requires use of IS to compare to NULL </li></ul></ul></ul><ul><ul><ul><li>QUOTED_IDENTIFIERS – Set to on indicates that double quotes identify object names </li></ul></ul></ul>
    10. 10. Deterministic <ul><li>An indexed view must be deterministic </li></ul><ul><ul><li>Result of calculation is the same with same inputs </li></ul></ul><ul><ul><li>DateAdd is deterministic </li></ul></ul><ul><ul><li>GetDate is non-deterministic </li></ul></ul>
    11. 11. Places to Consider An Index <ul><li>Joins and aggregations of large tables </li></ul><ul><li>Repeated patterns of queries (common WHERE clause) </li></ul><ul><li>Repeated aggregations on the same or overlapping sets of columns </li></ul><ul><li>Repeated joins of the same tables on the same keys </li></ul>
    12. 12. Indexes’ Downside <ul><li>Have another list that must be maintained when data changes </li></ul><ul><li>Can’t reference other views </li></ul><ul><li>Tables must be in same database </li></ul><ul><li>Cannot sort view in definition </li></ul><ul><ul><li>Can use ORDER BY when view referenced in SELECT </li></ul></ul><ul><li>Unique clustered index must be created before any other indexes can be created </li></ul>
    13. 13. Updateable Views <ul><li>Can update base tables through a view. </li></ul><ul><li>To have an updateable view, </li></ul><ul><ul><li>Can’t include a DISTINCT or TOP clause. </li></ul></ul><ul><ul><li>Field list can’t include an aggregate function. </li></ul></ul><ul><ul><li>Field list can’t include a calculated value. </li></ul></ul><ul><ul><li>Can’t include a GROUP BY or HAVING clause. </li></ul></ul><ul><ul><li>Can’t include the UNION operator. </li></ul></ul>
    14. 14. Updating rows using a view <ul><li>Use the UPDATE statement to update a table through a view </li></ul><ul><ul><li>Use the view name in the UPDATE clause </li></ul></ul><ul><ul><li>The view must be updatable (prior slide) </li></ul></ul><ul><ul><li>The UPDATE statement can’t update data in more than one table. </li></ul></ul>
    15. 15. WITH CHECK <ul><li>If WITH CHECK is used when the view is created, trying to change a row such that it wouldn’t be included in the view result will result in an error. </li></ul><ul><ul><li>If a view only returns rows where city = ‘Olympia’, and city is changed to ‘Lacey’ an error occurs </li></ul></ul>