ny Views
Me mwester +ftochas leS ve betel del pw&/s
= In some cases, it is not desirable for all users to see the
entire logical model (that is, all the actual relations stored in
the database.)
= Consider a person who needs to know an instructors name
and department, but not the salary. This person should see a
relation described, in SQL, by
select /D, name, dept_name
from instructor
ocolty ce-dis osotes ales User Toe
= A view provides a mechanism to hide certain data from the
view of certain users.
= Any relation that is not of the conceptual model but is made
visible to a user as a “virtual relation” is called a view.
Database System Concepts - 7" Edition 422 @Silberschatz, Korth and Sudarshanny View Definition
= A view is defined using the create view statement which
has the form renee See
create view v as < query expression >
where is any legal SQL expression.
The view name is represented by v.
= Once a view is defined, the view name can be used to refer
to the virtual relation that the view generates.
= View definition is not the same as creating a new relation
by evaluating the query expression
° Rather, a view definition causes the saving of an
expression; the expression is substituted into queries
using the view.
Database System Concepts - 7" Edition 423 @Silberschatz, Korth and SudarshanLy View Definition and Use
= A view of instructors without their salary
create view faculty as
select /D, name, dept_name
from instructor
= Find all instructors in the Biology department
select name
from faculty
where dept_name = 'Biology’
= Create a view of department salary totals
create view departments_total_salary(dept_name, total_salary) as
select dept_name, sum (salary)
from instructor
group by dept_name;
Database System Concepts - 7" Edition 424 @Silberschatz, Korth and Sudarshanny Views Defined Using Other Views
One view may be used in the expression defining another
view
A view relation v, is said to depend directly on a view
relation v. if v2 is used in the expression defining v,
A view relation v, is said to depend on view relation vz if
either v, depends directly to v. or there is a path of
dependencies from v, to v>
A view relation v is said to be recursive if it depends on
itself.
Database System Concepts - 7" Edition 425 @Silberschatz, Korth and SudarshanLy Views Defined Using Other Views
= create view physics_fall_2017 as
select course.course_id, sec_id, building, room_number
from course, section
where course.course_id = section.course_id
and course.dept_name = 'Physics'
and section.semester = ‘Fall’
and section.year = '2017';
= create view physics_fall_2017_watson as
select course_id, room_number
from physics_fall_2017
where building= ‘Watson’;
Database System Concepts - 7" Edition 426 @Silberschatz, Korth and SudarshanView Expansion
= Expand the view :
create view physics_fall_2017_watson as
select course_id, room_number
from physics_fall_2017
= To:
create view physics_fall_2017_watson as
select course_id, room_number
from (select course.course_id, building, room_number
from course, section
where course.course_id = section.course_id
and course.dept_name = 'Physics'
and section.semester = ‘Fall’
and section.year = '2017')
where building= ‘Watson’;
Database System Concepts - 7" Edition 427 @Silberschatz, Korth and Sudarshanuy
View Expansion (Cont.)
A way to define the meaning of views defined in terms of other
views.
Let view v, be defined by an expression e, that may itself
contain uses of view relations.
View expansion of an expression repeats the following
replacement step:
repeat
Find any view relation v; in e,
Replace the view relation v; by the expression defining v;
until no more view relations are present in e,
As long as the view definitions are not recursive, this loop will
terminate
Database System Concepts - 7" Edition 428 @Silberschatz, Korth and Sudarshanny Materialized Views
= Certain database systems allow view relations to be
physically stored.
* Physical copy created when the view is defined.
* Such views are called Materialized view:
= If relations used in the query are updated, the
materialized view result becomes out of date
* Need to maintain the view, by updating the view
whenever the underlying relations are updated.
Database System Concepts - 7" Edition 429 @Silberschatz, Korth and Sudarshanny Update of a View
= Add anew tuple to faculty view which we defined earlier
insert into faculty
values ('30765’, 'Green’, 'Music’);
= This insertion must be represented by the insertion into the
instructor relation
° Must have a value for salary.
= Two approaches
* Reject the insert
* Inset the tuple
('30765', 'Green’, ‘Music’, null)
into the instructor relation
Database System Concepts - 7" Edition 430 @Silberschatz, Korth and SudarshanLy Some Updates Cannot be Translated Uniquely
= create view instructor_info as
select /D, name, building
from instructor, department
where instructor.dept_name= department.dept_name;
= insert into instructor_info
values ('69987’, ‘White’, 'Taylor');
= Issues
* Which department, if multiple departments in Taylor?
* What if no department is in Taylor?
Database System Concepts - 7" Edition 431 @Silberschatz, Korth and Sudarshanny And Some Not at All
= create view history_instructors as
select *
from instructor
where dept_name= ‘History’;
= What happens if we insert
('25566', ‘Brown’, ‘Biology’, 100000)
into history_instructors?
Database System Concepts - 7" Edition 432 @Silberschatz, Korth and Sudarshanny View Updates in SQL
= Most SQL implementations allow updates only on simple
views
* The from clause has only one database relation.
* The select clause contains only attribute names of the
relation, and does not have any expressions, aggregates,
or distinct specification.
* Any attribute not listed in the select clause can be set to
null
* The query does not have a group by or having clause.
Database System Concepts - 7" Edition 433 @Silberschatz, Korth and Sudarshan