29
Module 11: Implementing Views Vidy a Vr at Ag ar wal . | MCT, MCSD

Module 11 Implementing Views

Embed Size (px)

Citation preview

8/9/2019 Module 11 Implementing Views

http://slidepdf.com/reader/full/module-11-implementing-views 1/29

8/9/2019 Module 11 Implementing Views

http://slidepdf.com/reader/full/module-11-implementing-views 2/29

Overview

Introduction to Views

Advantages of Views

Defining Views

Modifying Data Through Views

8/9/2019 Module 11 Implementing Views

http://slidepdf.com/reader/full/module-11-implementing-views 3/29

Introduction to Views

A View is a Virtual Table which consists of a subset of columns fromone 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 physicallystored data, but by its definition in terms of other named tables( Base Tables).

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

8/9/2019 Module 11 Implementing Views

http://slidepdf.com/reader/full/module-11-implementing-views 4/29

Creating a View

EmployeeView 

EmployeeView EmployeeView 

Lastname 

Lastname  Firstname 

Firstname 

Davolio

Fuller

Leverling

Davolio

Fuller

Leverling

Nancy

Andrew

Janet

Nancy

Andrew

Janet

Employees 

Employees Employees 

EmployeeID 

EmployeeID  LastName 

LastName  Firstname 

Firstname  Title 

Title 

1

2

3

1

2

3

Davolio

Fuller

Leverling

Davolio

Fuller

Leverling

Nancy

Andrew

Janet

Nancy

Andrew

Janet

~~~

~~~

~~~

~~~

~~~

~~~

UserUser’’s Views View

USE NorthwindGOCREATE VIEW EmployeeViewAS

SELECT LastName, FirstnameFROM Employees

USE NorthwindGOCREATE VIEW EmployeeViewAS

SELECT LastName, FirstnameFROM Employees

8/9/2019 Module 11 Implementing Views

http://slidepdf.com/reader/full/module-11-implementing-views 5/29

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

8/9/2019 Module 11 Implementing Views

http://slidepdf.com/reader/full/module-11-implementing-views 6/29

Example: View of Joined Tables

OrderID

OrderIDOrderID

1066310827104271045110515

1066310827104271045110515

CustomerID

CustomerIDCustomerID

BONAPBONAPPICCOQUICKQUICK

BONAPBONAPPICCOQUICKQUICK

~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~

RequiredDate

RequiredDateRequiredDate

1997-09-241998-01-261997-02-241997-03-051997-05-07

1997-09-241998-01-261997-02-241997-03-051997-05-07

ShippedDate

ShippedDateShippedDate

1997-10-031998-02-061997-03-031997-03-121997-05-23

1997-10-031998-02-061997-03-031997-03-121997-05-23

Orders Customers

ShipStatusViewUSE Northwind

GO

CREATE VIEW ShipStatusViewAS

SELECT OrderID, RequiredDate, ShippedDate,ContactName

FROM Customers c INNER JOIN Orders o

ON c.CustomerID = O.CustomerIDWHERE RequiredDate < ShippedDate

USE NorthwindGO

CREATE VIEW ShipStatusView

AS

SELECT OrderID, RequiredDate, ShippedDate,ContactName

FROM Customers c INNER JOIN Orders o

ON c.CustomerID = O.CustomerIDWHERE RequiredDate < ShippedDate

CustomerID

CustomerIDCustomerID

BONAPPICCOQUICK

BONAPPICCOQUICK

CompanyName

CompanyNameCompanyName

Bon app'Piccolo und mehr QUICK-Stop

Bon app'Piccolo und mehr QUICK-Stop

ContactName

ContactNameContactName

Laurence LebihanGeorg PippsHorst Kloss

Laurence LebihanGeorg PippsHorst Kloss

OrderID

OrderIDOrderID

102641027110280

102641027110280

1996-08-211996-08-291996-09-11

1996-08-211996-08-291996-09-11

ShippedDate

ShippedDateShippedDate

1996-08-231996-08-301996-09-12

1996-08-231996-08-301996-09-12

ContactName

ContactNameContactName

Laurence LebihanGeorg PippsHorst Kloss

Laurence LebihanGeorg PippsHorst Kloss

8/9/2019 Module 11 Implementing Views

http://slidepdf.com/reader/full/module-11-implementing-views 7/29

Altering and Dropping Views

Altering Views

Retains assigned permissions

Causes new SELECT statement and options to replace

existing definition Dropping Views

USE NorthwindGOALTER VIEW EmployeeViewASSELECT LastName, FirstName, ExtensionFROM Employees

USE NorthwindGOALTER VIEW EmployeeViewAS

SELECT LastName, FirstName, ExtensionFROM Employees

DROP VIEW ShipStatusViewDROP VIEW ShipStatusView

8/9/2019 Module 11 Implementing Views

http://slidepdf.com/reader/full/module-11-implementing-views 8/29

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’

8/9/2019 Module 11 Implementing Views

http://slidepdf.com/reader/full/module-11-implementing-views 9/29

Hiding View Definitions

Use the WITH ENCRYPTION Option

USE NorthwindGOCREATE VIEW customers_view

WITH ENCRYPTION

ASSELECT customerid,companyname

FROM customersGO

USE NorthwindGOCREATE VIEW customers_view

WITH ENCRYPTION

ASSELECT customerid,companynameFROM customers

GO

Sp_helptext ‘customers_view’The object comments have been encrypted.

8/9/2019 Module 11 Implementing Views

http://slidepdf.com/reader/full/module-11-implementing-views 10/29

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 NotReferenced in the View

Are Verified If the WITH CHECK OPTION Has BeenSpecified

8/9/2019 Module 11 Implementing Views

http://slidepdf.com/reader/full/module-11-implementing-views 11/29

With check Option

CREATE VIEW svc

WITH ENCRYPTIONas select * from student

where age between 18 and 20

WITH CHECK OPTION

8/9/2019 Module 11 Implementing Views

http://slidepdf.com/reader/full/module-11-implementing-views 12/29

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 thequery defining the view.

For example, a query can be created, defining a view that retrieves allrows 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 doesnot conform to the criteria set by the view.

However, the WITH CHECK OPTION clause forces all data modificationstatements executed against the view to adhere to the criteria set withinthe SELECT statement defining the view. If you use this clause, rowscannot be modified in a way that causes them to disappear from the

view. Any modification that would cause this to happen is canceled andan error is displayed.

8/9/2019 Module 11 Implementing Views

http://slidepdf.com/reader/full/module-11-implementing-views 13/29

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.

8/9/2019 Module 11 Implementing Views

http://slidepdf.com/reader/full/module-11-implementing-views 14/29

Check Your Understanding.

8/9/2019 Module 11 Implementing Views

http://slidepdf.com/reader/full/module-11-implementing-views 15/29

Q.1 What is View.?

8/9/2019 Module 11 Implementing Views

http://slidepdf.com/reader/full/module-11-implementing-views 16/29

Q.2. What is Base Table. ?

8/9/2019 Module 11 Implementing Views

http://slidepdf.com/reader/full/module-11-implementing-views 17/29

Q.3. What are the advantages of a View.?

8/9/2019 Module 11 Implementing Views

http://slidepdf.com/reader/full/module-11-implementing-views 18/29

Q.4.Views are not autonomous and do not exist by their own right.?

1. True

2. False

8/9/2019 Module 11 Implementing Views

http://slidepdf.com/reader/full/module-11-implementing-views 19/29

Q.5. What is the Syntax to create a View.

8/9/2019 Module 11 Implementing Views

http://slidepdf.com/reader/full/module-11-implementing-views 20/29

Q.6. What is With Encryption.?

8/9/2019 Module 11 Implementing Views

http://slidepdf.com/reader/full/module-11-implementing-views 21/29

Q.7. What is With Check Option.

8/9/2019 Module 11 Implementing Views

http://slidepdf.com/reader/full/module-11-implementing-views 22/29

Q.8. The View created by using With Encryption can beDecrypted .?

1. Yes

2. No

8/9/2019 Module 11 Implementing Views

http://slidepdf.com/reader/full/module-11-implementing-views 23/29

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.

8/9/2019 Module 11 Implementing Views

http://slidepdf.com/reader/full/module-11-implementing-views 24/29

Q.10. If columns names are not specified explicitly in theView Definition statement then the view inherits thecolumn names of the______________ 

1. System catalog

2. Sp_depends

3. Source of the View

4. None of the above.

8/9/2019 Module 11 Implementing Views

http://slidepdf.com/reader/full/module-11-implementing-views 25/29

Q.11. The Clause ‘ WITH CHECK OPTION ’ indicates thatUpdate and Insert operations against the view are tobe checked to ensure that the Updated or Inserted

rows dissatisfy the view definition condition.

1. True

2. False

8/9/2019 Module 11 Implementing Views

http://slidepdf.com/reader/full/module-11-implementing-views 26/29

Q.12. You are executing performance optimization onyour company's market research database. On thetblConsumers table, you have a view namedvConsumerData. You would like to see the originalstatement 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

8/9/2019 Module 11 Implementing Views

http://slidepdf.com/reader/full/module-11-implementing-views 27/29

Q.13. Which of the following statements is true aboutmodifying data using a view?

1. The data from multiple underlying tables can bemodified 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.

8/9/2019 Module 11 Implementing Views

http://slidepdf.com/reader/full/module-11-implementing-views 28/29

Review

Introduction to Views

Advantages of Views

Defining Views

Modifying Data Through Views

8/9/2019 Module 11 Implementing Views

http://slidepdf.com/reader/full/module-11-implementing-views 29/29

It isCourage and Character

that is the deadly combinationfor Success

Thank You.