Lesson 1 of 3 Views and User-Defined Functions

Views, View Options and Indexed Views

This lesson explains how views work, the SCHEMABINDING and CHECK OPTION settings, updatable views and indexed views.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain the effect of SCHEMABINDING and WITH CHECK OPTION.
  • Identify when a view is updatable.
  • Describe the requirements and trade-offs of indexed views.

Creating views

CREATE VIEW stores a SELECT statement under a name. Queries against the view are expanded into the underlying query, so a view adds no storage and its performance is that of the query it contains. Views simplify complex joins, give applications a stable interface while base tables change, and expose only selected columns and rows. CREATE OR ALTER VIEW creates or replaces a view in one statement. A view cannot contain ORDER BY unless TOP or OFFSET is used, and even then the order of rows returned from the view is not guaranteed.

View options

Three options change a view's behaviour. WITH SCHEMABINDING requires two-part names and prevents dropping or altering referenced columns in ways that affect the view. WITH CHECK OPTION rejects INSERT and UPDATE statements through the view that would produce rows the view cannot see. WITH ENCRYPTION obfuscates the definition in metadata but is not real protection.

CREATE OR ALTER VIEW Sales.vOpenOrdersEU
WITH SCHEMABINDING
AS
SELECT o.OrderID, o.CustomerID, o.OrderDate, o.TotalDue, c.Country
FROM Sales.Orders AS o
JOIN Sales.Customer AS c ON c.CustomerID = o.CustomerID
WHERE o.Status = 'Open' AND c.Region = 'EU'
WITH CHECK OPTION;

A view is updatable when a modification affects columns from only one base table and does not target aggregated or computed columns. Other modifications need an INSTEAD OF trigger on the view.

Match the pairs

View options

Exam skills 1.2.1

Match each view option with its effect.

Log in to try this activity Interactive activities are free with an account.

Indexed views

An indexed view materializes its result by adding a unique clustered index to a schema-bound view. The engine maintains the stored result as base tables change, so aggregations and joins that run repeatedly are read from the index. Requirements include SCHEMABINDING, deterministic expressions, COUNT_BIG(*) when GROUP BY is used, and no outer joins, subqueries or non-deterministic functions. Every write to the base tables also updates the view, so indexed views suit read-heavy data. In Azure SQL Database, the optimizer can match indexed views automatically; the NOEXPAND hint forces their use.

CREATE VIEW Sales.vDailyRevenue WITH SCHEMABINDING AS
SELECT OrderDate, SUM(TotalDue) AS Revenue, COUNT_BIG(*) AS OrderCount
FROM Sales.Orders
GROUP BY OrderDate;

CREATE UNIQUE CLUSTERED INDEX UCX_vDailyRevenue ON Sales.vDailyRevenue (OrderDate);
Spot the bug

An indexed view that will not build

Exam skills 1.2.1

Find the lines that stop this view from being indexed on OrderID.

Log in to try this activity Interactive activities are free with an account.
Worked example

A view for regional staff

EU support staff must see and edit only open orders of EU customers, without SELECT permission on the base tables, and must not be able to change an order so that it no longer belongs to the EU set. What should the developer create?

Create a view that filters open EU orders, add WITH CHECK OPTION, and grant the support role SELECT and UPDATE on the view only. Ownership chaining allows access through the view when the view and tables share an owner, and CHECK OPTION rejects updates that would move a row out of the view's filter. Row-Level Security could also restrict rows, but the requirement to block updates that leave the subset is exactly what CHECK OPTION provides.

Check yourself

Which aggregate must an indexed view with GROUP BY include?

Show answer

COUNT_BIG(*), which the engine uses to maintain the view incrementally.

Exam Tips

Remember for the exam

  • Stop base-table changes from breaking a view: WITH SCHEMABINDING.
  • Block modifications that move rows outside the view: WITH CHECK OPTION.
  • Materialize repeated aggregations: an indexed view with a unique clustered index.
  • Indexed views with GROUP BY need COUNT_BIG(*).