Sie sind auf Seite 1von 17

Views

TEAM MENTOR – GOPAL NARAYANAM, JAKKULA PRUDVIKA

TEAM MEMBERS – MANISH BHARDWAJ , HIMANSHU LAL

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.

• It can be used for following purposes:

 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.

• Syntax to create a view

CREATE VIEW [ schema_name . ] view_name [ (column [ ,...n ] ) ]

[ WITH <Schema Binding> [ ,...n ] ]

AS select_statement

[ WITH CHECK OPTION ]

[;]

5
Why do you create a View in a database?

• Views can hide complexity


If you have a query that requires joining several tables, or has complex logic or calculations, you can code all that logic into a view, then select from the view
just like you would a table

• Views can be used as a security mechanism


A view can select certain columns and/or rows from a table, and permissions set on the view instead of the underlying tables. This allows surfacing only the
data that a user needs to see.

• Views can simplify supporting legacy code


If you need to refactor a table that would break a lot of code, you can replace the table with a view of the same name. The view provides the exact same
schema as the original table, while the actual schema has changed. This keeps the legacy code that references the table from breaking, allowing you to
change the legacy code at your leisure.

6
Restrictions in select clause

The SELECT clauses in a view definition cannot include the following:

• An ORDER BY clause, unless there is also a TOP clause in the select list of the SELECT statement.

• The INTO keyword

• A reference to a temporary table or a table variable.

7
Altering view

• The ALTER VIEW command allows you to modify a view.

• ALTER VIEW does not affect dependent stored procedures or triggers and does not change permissions.

• Syntax to alter a view:


ALTER VIEW [ schema_name . ] view_name [ ( column [ ,...n ] ) ]

[ WITH <view_attribute> [ ,...n ] ]

AS select_statement

[ WITH CHECK OPTION ] [ ; ]

<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.

• Restrictions for updating data in views:


• Any modifications, including UPDATE, INSERT, and DELETE statements, must reference columns from only one base table.

• 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:

• An aggregate function: AVG, COUNT, SUM, MIN, MAX, GROUPING

• 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

• Used to insert data into underlying table through view


• Syntax for inserting data through view:
INSERT INTO [ schema_name . ] view_name [ (column [ ,...n ] ) ] VALUES (column_value [ ,...n ] );

11
Delete

• Used to delete from underlying table through view


• Syntax for deleting data through view:
DELETE FROM [ schema_name . ] view_name WHERE <condition>;

12
Renaming Views
• We can rename views by two methods
1. By using Object Explorer
2. By using T SQL Command

Syntax :- EXEC sp_rename


@objname = 'old_name',
@newname = 'new_name';

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

• Removes one or more views from the current database.


• DROP VIEW can be executed against indexed views.
• Syntax for dropping a view:
DROP VIEW [ IF EXISTS ] [ schema_name . ] view_name [ ...,n ] [ ; ]
Arguments:
• IF EXISTS
Conditionally drops the view only if it already exists.
• schema_name
Is the name of the schema to which the view belongs.
• view_name
Is the name of the view to remove.

16
Discussion

Das könnte Ihnen auch gefallen