Sie sind auf Seite 1von 29

Module 11:

Implementing Views

Vidya Vrat Agarwal. | MCT, MCSD


Overview

 Introduction to Views
 Advantages of Views
 Defining Views
 Modifying Data Through Views
Introduction to Views

A View is a Virtual Table which consists of a subset of columns from


one or more tables.

i.e. View is an object that derives its data from one or more tables
( Base tables or Underlying Tables).

A View is a Named Table that is represented, not by its own physically


stored data, but by its definition in terms of other named tables
( Base Tables).

View is a “ Virtual table”, deriving its data from “ Base Tables”


Creating a View
Employees
Employees
EmployeeID
EmployeeID LastName
LastName Firstname
Firstname Title
Title
11 Davolio
Davolio Nancy
Nancy ~~~
~~~
22 Fuller
Fuller Andrew
Andrew ~~~
~~~
33 Leverling
Leverling Janet
Janet ~~~
~~~
USE
USE Northwind
Northwind
GO
GO
CREATE
CREATE VIEW
VIEW EmployeeView
EmployeeView
AS
AS
SELECT
SELECT LastName,
LastName, Firstname
Firstname
FROM Employees
FROM Employees

EmployeeView
EmployeeView
Lastname
Lastname Firstname
Firstname
Davolio
Davolio Nancy
Nancy User’s View
Fuller
Fuller Andrew
Andrew
Leverling
Leverling Janet
Janet
Advantages of Views

 Focus the Data for Users


 Focus on important or appropriate data only
 Limit access to sensitive data
 Mask Database Complexity
 Hide complex database design
 Simplify complex queries
 Simplify Management of User Permissions
 Improve Performance
Example: View of Joined Tables
Orders Customers
OrderID CustomerID
OrderID CustomerIDRequiredDate
RequiredDateShippedDate
ShippedDate CustomerID
CustomerID CompanyName
CompanyName ContactName
ContactName
10663
10663 BONAP
BONAP 1997-09-24
~~~
1997-09-24
~~~ 1997-10-03
1997-10-03 BONAP
BONAP Bon
Bonapp'
app' Laurence
LaurenceLebihan
Lebihan
10827
10827 BONAP
BONAP 1998-01-26
~~~
1998-01-26
~~~ 1998-02-06
1998-02-06 PICCO
PICCO Piccolo
Piccolound
undmehr
mehr Georg Pipps
Georg Pipps
10427
10427 PICCO
PICCO 1997-02-24
~~~
1997-02-24
~~~ 1997-03-03
1997-03-03 QUICK
QUICK QUICK-Stop
QUICK-Stop Horst
HorstKloss
Kloss
10451
10451 QUICK
QUICK 1997-03-05
~~~
1997-03-05
~~~ 1997-03-12
1997-03-12
10515
10515 QUICK
QUICK 1997-05-07
~~~
1997-05-07
~~~ 1997-05-23
1997-05-23

USE
USE Northwind
Northwind ShipStatusView
GO
GO
CREATE
CREATE VIEW
VIEW ShipStatusView
ShipStatusView
AS OrderID
AS OrderID ShippedDate
ShippedDate ContactName
ContactName
SELECT
SELECT OrderID,
OrderID, RequiredDate,
RequiredDate, ShippedDate,
ShippedDate, 10264 1996-08-23
1996-08-21
10264 1996-08-21Laurence
1996-08-23 LaurenceLebihan
Lebihan
ContactName
ContactName 10271 1996-08-30
1996-08-29 Georg Pipps
10271 1996-08-30
1996-08-29 Georg Pipps
FROM
FROM Customers cc INNER
Customers INNER JOIN
JOIN Orders
Orders oo 10280
10280 1996-09-12
1996-09-11
1996-09-11Horst
1996-09-12 HorstKloss
Kloss
ON
ON c.CustomerID
c.CustomerID == O.CustomerID
O.CustomerID
WHERE RequiredDate < ShippedDate
WHERE RequiredDate < ShippedDate
Altering and Dropping Views

 Altering Views
USE
USE Northwind
Northwind
GO
GO
ALTER
ALTER VIEW
VIEW EmployeeView
EmployeeView
AS
AS
SELECT
SELECT LastName,
LastName, FirstName,
FirstName, Extension
Extension
FROM Employees
FROM Employees

 Retains assigned permissions


 Causes new SELECT statement and options to replace
existing definition
 Dropping Views
DROP
DROP VIEW
VIEW ShipStatusView
ShipStatusView
Locating View Definition Information

 Locating View Definitions


Sp_helptext ‘View_Name’

 Locating View Dependencies


 Lists objects upon which view depends
 Lists objects that depend on a view
Sp_depends ‘View_Name’
Hiding View Definitions

 Use the WITH ENCRYPTION Option

USE
USE Northwind
Northwind
GO
GO
CREATE
CREATE VIEW
VIEW customers_view
customers_view
WITH ENCRYPTION
WITH ENCRYPTION
AS
AS
SELECT
SELECT customerid,companyname
customerid,companyname
FROM
FROM customers
customers
GO
GO

Sp_helptext ‘customers_view’
The object comments have been encrypted.
Modifying Data Through Views

 Cannot Affect More Than One Underlying Table


 Cannot Be Made to Certain Columns
 Can Cause Errors If They Affect Columns That Are Not
Referenced in the View
 Are Verified If the WITH CHECK OPTION Has Been
Specified
With check Option

CREATE VIEW svc


WITH ENCRYPTION
as select * from student
where age between 18 and 20
WITH CHECK OPTION
With Check Option
By default, as rows are added or updated through a view, they disappear
from the scope of the view when they no longer fall into the criteria of the
query defining the view.
For example, a query can be created, defining a view that retrieves all
rows from a table where the employee's salary is less than $30,000. If
the employee's salary is increased to $32,000, then querying the view no
longer displays that particular employee because his or her salary does
not conform to the criteria set by the view.
However, the WITH CHECK OPTION clause forces all data modification
statements executed against the view to adhere to the criteria set within
the SELECT statement defining the view. If you use this clause, rows
cannot be modified in a way that causes them to disappear from the
view. Any modification that would cause this to happen is canceled and
an error is displayed.
With Check Option

insert into svc


values('1111119','abc','Male',21,'Delhi')

Server: Msg 550, Level 16, State 1, Line 1


The attempted insert or update failed because the target view either
specifies WITH CHECK OPTION or spans a view that specifies
WITH CHECK OPTION and one or more rows resulting from the
operation did not qualify under the CHECK OPTION constraint.
The statement has been terminated.
Check Your Understanding.
Q.1 What is View.?
Q.2. What is Base Table. ?
Q.3. What are the advantages of a View.?
Q.4.Views are not autonomous and do not exist by their
own right.?

1. True
2. False
Q.5. What is the Syntax to create a View.
Q.6. What is With Encryption.?
Q.7. What is With Check Option.
Q.8. The View created by using With Encryption can be
Decrypted .?

1. Yes
2. No
Q.9. Views can be used for ________
1. Providing row and column level security.
2. Ensuring efficient access to data.
3. Masking data complexity from user.
4. Helps to work as Physical Table for User.
5. All of the Above.
Q.10. If columns names are not specified explicitly in the
View Definition statement then the view inherits the
column names of the______________

1. System catalog
2. Sp_depends
3. Source of the View
4. None of the above.
Q.11. The Clause ‘ WITH CHECK OPTION ’ indicates that
Update and Insert operations against the view are to
be checked to ensure that the Updated or Inserted
rows dissatisfy the view definition condition.

1. True
2. False
Q.12. You are executing performance optimization on
your company's market research database. On the
tblConsumers table, you have a view named
vConsumerData. You would like to see the original
statement used to create the view. What statement
should you execute?
1. sp_helptext vConsumerData
2. sp_helptext tbl_Consumers
3. sp_depends v_ConsumerData
4. sp_depends tbl_Consumers
Q.13. Which of the following statements is true about
modifying data using a view?

1. The data from multiple underlying tables can be


modified simultaneously.
2. Changes can be made to computed columns.
3. The view cannot be defined on a single table.
4. Modifications to data in a view affect the base table.
Review

 Introduction to Views
 Advantages of Views
 Defining Views
 Modifying Data Through Views
It is
Courage and Character
that is the deadly combination
for Success

Thank You.

Das könnte Ihnen auch gefallen