Beruflich Dokumente
Kultur Dokumente
JAN 16 , 2018
Introduction to Views:
Contents
• Creating views
• Altering views
• Updating views
• Insert into views
• Deleting from views
• Renaming views
• Schemabinding
• Check Option
• Dropping views
3
Views
• A view is a virtual table. It can derive its data from one or more than one table columns of same database or
different database.
To focus, simplify, and customize the perception each user has of the database.
Views are used for security as it display only selected data without granting the users permissions to
directly access the base tables of the view.
To provide a backward compatible interface to emulate a table whose schema has changed.
4
Creating Views:
• Creates a virtual table whose contents (columns and rows) are defined by a query.
AS select_statement
[;]
5
Why do you create a View in a database?
6
Restrictions in select clause
• An ORDER BY clause, unless there is also a TOP clause in the select list of the SELECT statement.
7
Altering view
• ALTER VIEW does not affect dependent stored procedures or triggers and does not change permissions.
AS select_statement
<view_attribute> ::=
[ ENCRYPTION ]
[ SCHEMABINDING ]
[ VIEW_METADATA ]
8
Updating views
• We can modify the data of an underlying base table through a view by using insert, delete, update statements.
• The columns being modified in the view must directly reference the underlying data in the table columns. The columns cannot
be derived in any other way, such as through the following:
• A computation. The column cannot be computed from an expression that uses other columns. Columns that are formed
by using the set operators UNION, UNION ALL, CROSSJOIN, EXCEPT, and INTERSECT amount to a computation and are also
not updatable.
• The columns being modified are not affected by GROUP BY, HAVING, or DISTINCT clauses.
• TOP is not used anywhere in the select_statement of the view together with the WITH CHECK OPTION clause.
9
Update
• Updates data in the underlying table through view
• Syntax for updating data through view:
UPDATE < view_name > SET <column1>=<value1>,<column2>=<value2>,..... WHERE <condition>;
10
Insert
11
Delete
12
Renaming Views
• We can rename views by two methods
1. By using Object Explorer
2. By using T SQL Command
13
WITH SchemaBinding
• Why you use the SchemaBinding keyword while creating a View or Function you bind the structure of any
underlying tables or views. So what does that mean?
• It means that as long as that schemabound object exists as a schemabound object (i.e you don’t remove
schemabinding) you are limited in changes that can be made to the tables or views that it refers to.
• When SCHEMABINDING is specified, the base table or tables cannot be modified in a way that would affect the
view definition.
• The view definition itself must first be modified or dropped to remove dependencies on the table that is to be
modified.
• When creating a schemabound view or function you have to use the two part name (include the schema
name) for any tables or views you reference within it.
14
WITH CHECK OPTION
• The WITH CHECK OPTION is a CREATE VIEW statement option. The purpose of the WITH CHECK OPTION is to
ensure that all UPDATE and INSERTs satisfy the condition(s) in the view definition.
• If they do not satisfy the condition(s), the UPDATE or INSERT returns an error.
• When a row is modified through a view, the WITH CHECK OPTION makes sure the data remains visible through
the view after the modification is committed.
15
Drop View
16
Discussion