ViewsSimilar to an Access query Predefined SELECT statement At most basic level specifies which columns and rows to returnProvides a virtual table SELECT * FROM Customers represents rows and columns but isn’t for storing data
Reasons for ViewsReduce database complexity‘Hide’ sensitive dataImprove query performance
Reduce ComplexityProvide access to data from multiple tables Normalization doesn’t get in the wayLimit number of columns returned Instead of returning all columns just return ones commonly used
Hiding DataProvide only columns that are appropriateProvide only rows that are appropriateCan have multiple views on one table with different fields and criteria for different user groupsCan also hide table and database organization
Ease of UseCan provide access to current information List only students who are currently enrolled in a courseCan organize data for common reports List students with assignments for a section
View OptionsEncryption prevents viewing the SQL statementSchema binding ties the view to objects it depends on Can’t drop a table – or column – that’s referenced in a bound viewCan sort results Requires use of TOP option Can also use ORDER BY in SELECT where view is used
PerformanceBy default views run just as a query processed from command line Adds second step to execution: select from view execute view itselfCan add an index to speed execution
Indexed ViewsSpeeds up execution of query, but has rules: View must be schema-bound View can’t reference other views (only tables & functions) Two-part names required, and must be same owner as view (dbo.Students)
Indexed Views (cont.) View and source tables must be in same database Functions used must be deterministic ANSI_NULLS and QUOTED_IDENTIFIERS must be on when view and source tables created ANSI_NULL – Set to on requires use of IS to compare to NULL QUOTED_IDENTIFIERS – Set to on indicates that double quotes identify object names
DeterministicAn indexed view must be deterministic Result of calculation is the same with same inputs DateAdd is deterministic GetDate is non-deterministic
Places to Consider An IndexJoins and aggregations of large tablesRepeated patterns of queries (common WHERE clause)Repeated aggregations on the same or overlapping sets of columnsRepeated joins of the same tables on the same keys
Indexes’ DownsideHave another list that must be maintained when data changesCan’t reference other viewsTables must be in same databaseCannot sort view in definition Can use ORDER BY when view referenced in SELECTUnique clustered index must be created before any other indexes can be created
Updateable ViewsCan update base tables through a view.To have an updateable view, Can’t include a DISTINCT or TOP clause. Field list can’t include an aggregate function. Field list can’t include a calculated value. Can’t include a GROUP BY or HAVING clause. Can’t include the UNION operator.
Updating rows using a viewUse the UPDATE statement to update a table through a view Use the view name in the UPDATE clause The view must be updatable (prior slide) The UPDATE statement can’t update data in more than one table.
WITH CHECKIf 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. If a view only returns rows where city = ‘Olympia’, and city is changed to ‘Lacey’ an error occurs
A particular slide catching your eye?
Clipping is a handy way to collect important slides you want to go back to later.