3. Views
A view is a virtual table whose contents
are defined by a query
CREATE VIEW [ schema_name . ] view_name [ (column [ ,...n ] ) ]
[ WITH <view_attribute> [ ,...n ] ]
AS select_statement
[ WITH CHECK OPTION ]
[ ; ]
<view_attribute> ::=
{
[ ENCRYPTION ]
[ SCHEMABINDING ]
[ VIEW_METADATA ]
}
4. Views
• Query is eventually applied to the underlying object
• Views can be used for
• Simplification and customization
• Security
• Backward compatibility
• The select statement cannot include
• ORDER BY clause, unless it is used with TOP or OFFSET
• Reference to temporary table or variable
• INTO and OPTION keywords
6. CHAPTER
• CHECK OPTION
• The CHECK OPTION in the SELECT statement of a
CREATE VIEW statement allows the user to specify rules
that any modifications against the view must be
compliant with.
• Any modifications that happen in the underlying tables
that violate the rules within the CHECK OPTION are not
visible in the view.
7. CHAPTER
• SCHEMABINDING
• The SCHEMABINDING option, binds the view to the
schema of the underlying tables.
• The SCHEMABINDING option can be used only with
objects within the same database and in the SELECT
statement the participating objects must be referenced
as (schema.object).
• Other views or tables cannot be altered or dropped if
they are participating in schemabinded views.
8. CHAPTER
• ENCRYPTION and VIEW_METADATA
• Encrypts the entries in sys.syscomments that contain the
text of the CREATE VIEW statement. Using WITH
ENCRYPTION prevents the view from being published
as part of SQL Server replication.
11. • Views through which the data of the underlying tables
can be modified.
• Managing security privileges across multiple tables
easier.
• User can modify underlying table through the view, f he haw the
corresponding permission on the view.
Updatable Views
12. • The modifications in an UPDATE, DELETE or INSERT statement can only
reference one underlying table at a time.
• The underlying columns to be modified, must be directly referenced by the
columns of the view. Cannot update columns that are derived by aggregate
functions or other computations.
• TOP clause is not present in the SELECT statement of the view.
• The columns being modified are not affected by GROUP BY, HAVING, or
DISTINCT clauses.
• Rule of thumb: The Database engine must be able to UNAMBIGUOUSLY trace
modifications from the view to the underlying table.
Restrictions
13. • The above restrictions can be avoided by creating an INSTEAD OF trigger on
the view.
• The trigger, explicitly specifies the actions to be taken if a data modification
statement is to be made on the view.
• By using an INSTEAD OF trigger, every view can be made updatable.
Bypassing the restrictions
CREATE TRIGGER <trigger_name>
ON <view>
INSTEAD OF
{INSERT, UPDATE, DELETE}
AS <SQL_code_here>;
16. • Indexed View: a view with a unique clustered index on it
• Indexed views are used for performance tuning
• An indexed view can be updatable
• Query optimizer can use the index to speed up queries
• Additional non-clustered indexes can be created
• Indexed views affect insert and update operations on
base tables
Indexed Views
17. • Verify SET options for session and underlying tables
• Check that the view definition is deterministic
• Create the view with SCHEMABINDING
• Create a unique clustered index on the view
Creating an Indexed View
SET OPTIONS REQUIRED VALUE
ANSI_NULLS ON
ANSI_PADDING ON
ANSI_WARNINGS ON
ARITHABORT ON
CONCAT_NULL_YIEDS_NULL ON
NUMERIC_ROUNDABORT OFF
QUOTED IDENTIFIER ON
18. • All expressions in the select list, the WHERE and the
GROUP BY clause must be deterministic
• Deterministic expression: returns the same result every
time evaluated on the same input
• Precise expression: do not contain float expressions
• Float expression can only be non-key columns
• Only precise deterministic expressions can participate in
key columns.
Deterministic Views and Precise Columns
19. • If GROUP BY is present
• the definition must contain COUNT_BIG(*)
• must not contain HAVING
• the key can reference only columns specified in the GROUP BY
clause
• CLR functions can appear in the select list, but cannot
be part of the key
• The view cannot reference objects in other databases
Restrictions
20. • The statement must not contain the following elements
Restrictions
COUNT DISTINCT * MIN, MAX
ORDER BY TOP OFFSET STDEV, STDEVP, VAR, VARP, AVG
OUTER joins Self-joins UNION,EXCEPT,INTERSECT CUBE, ROLLUP, GROUPING SETS
Subquery CTE Derived table table-valued functions
CLR user-defined aggregate
function
PIVOT, UNPIVOT APPLY text, ntext, image, XML, filestream
columns
Full-text predicates TABLESAMPLE Table variables ROWSET functions
SUM function on nullable
expression
Sparse column
sets
CHECKSUM_AGG OVER clause, which includes ranking
or aggregate window functions
… and some more restrictions and requirements
https://msdn.microsoft.com/en-us/library/ms191432.aspx#Restrictions