Module05

20
Introduction to Views Module 5

description

 

Transcript of Module05

Page 1: Module05

Introduction to Views

Module 5

Page 2: Module05

2

• Introduction to views

• Advantages of views

• To Create Views

• To Alter Views

• To Drop Views

• To Rename Views

• View with Check Option

• How to get the information on view

Contents

Page 3: Module05

3

Objectives

At the end of this presentation you should be able to:• Understand the concept of views• Understand why views are required in a database• Understand how to create and work with the views • Understand how to get information about a view

Page 4: Module05

4

Introduction to Views

• View is a database object which contains query.• A view is often referred to as a virtual table i.e..,

a view looks like a table and is referred like a table.

• They do not contain data like tables.• They can be used to join two tables in a

database and present the underlying data as if the data was coming from a single table.

Page 5: Module05

5

Introduction to Views (Continued)

• View can be used as a security mechanism to restrict the data available to the end users

• Views can also be used to store and reuse the complex query which generates the reports.

• Views hides the underlying complex database structure.

Page 6: Module05

6

Advantages of Views

A view provides following advantages:• Focuses data for users.• Hides data complexity.• Simplifies permission management.• Organizes data for export to other applications.• Reduces object size.

Page 7: Module05

7

Example of a Views

Publishers

Pubid Pubname City State Country

0736 New Moon Boston MA CHICAGO

0877 Binnet Washington DC USA

1389 Algodata Berkely CA USA

1622 Five lake Chicago IL PARIS

ViewPublishers

Pubid Pubname

0736 Binnet

0877 Algodata

1389 Five lake

This example shows creating a view called ‘ViewPublishers’ from the Publishers table.

This view contains two columns ‘Pubid’ and ‘Pubname’ which are taken from the table. The actual data will be in the Publishers table, which can be viewed and manipulated from the ‘ViewPublishers’ view.

Page 8: Module05

8

To Create ViewsSyntax:

Example to create view from publishers table

Example to create a view from Publishers table for city “paris”

CREATE VIEW <ViewName> AS <Select Statement>

CREATE VIEW ViewPublishers

As SELECT * FROM Publishers

CREATE VIEW ViewPublishers

AS SELECT pubid, pubname , city FROM Publishers WHERE city =‘paris’

Page 9: Module05

9

Simple views

• Simple View is also known as a normal view • It contains data only from one table • It won’t contain JOINS or GROUP By statements• The underlying data from the base tables can be

updated using an UPDATE statement.

Example CREATE VIEW ViewPublisher

AS SELECT * FROM Publishers

This example creates a simple view ViewPublisher which selects all the data from Publishers Table

Page 10: Module05

10

Complex Views

• It will be accessed from multiple tables

• Normally a complex view will contain joins

• It contains group functions to display values from two tables.

• It cannot be updated using UPDATE Clause

The example creates a complex view Dept_vw which selects the department name, minimum salary, maximum salary, and average salary.It uses three Aggregate functions and it joins two tables Employee and Department.

CREATE VIEW Dept_vw (deptname,minSalary,maxSalary,avgSalary) AS SELECT d.deptName,min(e.salary),max(e.salary),avg(e.salary) FROM Employee e, Department d WHERE e.deptId = d.deptId GROUP BY d.deptName

Page 11: Module05

11

View With Check option

• With Check option specifies the level of checking to be done while inserting or modifying data through view

• If the view in read-only view Check option cannot be used• It allows integrity constraints and data validation checks to

be enforced on data begin Inserted or Updated

CREATE VIEW empdept_vw as select empno,ename,deptno from emp where deptno =30 With check option;

The example uses ‘With Check Option’ clause, which will restrict the insertion of a row that cannot be selected by the view

Page 12: Module05

12

To Alter Views

• View can be modified without dropping it.• This ensures that the permissions on the view are not

lost • View can be modified without affecting it’s dependent

objects such as triggers and procedures

ALTER VIEW ViewPublishers As

SELECT pubid, pubname, pubcity, country From publishers

Alters the view to add publishers’ country column of the Publishers table

Page 13: Module05

13

To Drop Views

• View can be dropped from a database by using DROP VIEW

• When a view is dropped, it has no side effects on the underlying table (s).

DROP VIEW <ViewName>

Page 14: Module05

14

To Rename Views

• View can be renamed without having to drop it.• Only requirement is that views must be in current

database• A view can be renamed only by its owner• It can also be renamed by the owner of the

database

sp_rename <Old_ViewName>,<New_ViewName>

sp_rename Viewpublisher, VpubDetails

Page 15: Module05

15

To Get Information on view

• There are some system stored procedures that help retrieve information on a view :– sp_help : This can be used to find information about view

or any object in the database. It returns properties such as created date,column names , foreign-key constraints etc.

– sp_depends: This displays the information about the database object dependencies. Here it can be used to find the dependencies of the View.

– sp_helptext : This is used to display the actual text that was used to create an object in the database. This information is read from syscomments table.

Page 16: Module05

16

To Get Information on view (Continued)

– sp_columns- It returns information for the columns referred in specified views.• Sysobjects (table) - stores the complete details of

the view.• Syscolumns(table) - stores the names of the

columns defined in the view.

Page 17: Module05

17

Key Points

• Views are virtual tables • Views provide security of data • Views can be created with the CREATE VIEW statement• A view can be modified with the ALTER View statement • A view can be dropped with the DROP View statement• Views conceal complexity of queries which contain

complicated queries or sub queries• Normally a complex view will contain joins• Views can be created using “with check option”

Page 18: Module05

18

Activity Time (30 minutes)

• Activity: 5.1

Create a view named ‘VwDetails’ that retrieves the Title_ID, Title_Name, Type and Price from Title table and Publisher_ID, Publisher_Name from Publisher table. Also display the information of the view created and its dependencies.

• Activity: 5.2

Alter the view created in the Activity 1 to add another column Publisher_Email and after altering the view, display the definition of the altered view

• Activity: 5.3

Rename the view created in the Activity 1 as VwTitleDetails and at last drop the view.

Page 19: Module05

19

Activity Time (30 minutes)

• Activity: 5.4

Follow the Instructions below:

1) Create a view which retrieves the data from books and publishers table by using the following script.

create view VwTitleDetails2

as

select Title_ID, Title_Name,Type, publishers.publisher_name, from Title join Publisher on Title.Publisher_ID = Publisher. Publisher_ID

2) Now, Update the data using the view by using the following statement:

Update VwTitleDetails2

set price=21 where Title_ID = 'B001'

3) Notice the output.

Page 20: Module05

20

Questions & Comments