rdbms_it_lecture_view_via_sql
DESCRIPTION
DBMS_VIEW_ Contain SQLTRANSCRIPT
![Page 1: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/1.jpg)
Database Management
Systems
Views
Dr. Jeevani Goonetillake
UCSC
© 2010, University of Colombo School
of Computing
![Page 2: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/2.jpg)
Conceptual Layer
External LayerView View View
Conceptual Layer
Physical Layer
Base Tables
Stored Tables ...
© 2010, University of Colombo School
of Computing
![Page 3: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/3.jpg)
Architecture
Conceptual Layer
• The conceptual model is a logical
representation of the entire contents of the
Table
Table
database.
• The conceptual model is made up of base
tables.
• Base tables are “real” in that they contain
physical records.
© 2010, University of Colombo School
of Computing
![Page 4: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/4.jpg)
Architecture Conceptual Layer
Dept_Code Dep_Name Manager
SAL Sales 179
FIN Finance 857
Department
Employee
Emp_No Emp_Name Designation DOB Dept
179 Silva Manager 12-05-74 SAL
857 Perera Accountant 01-04-67 FIN
342 Dias Programmer 25-09-74 SAL
Employee
Base Tables© 2010, University of Colombo School
of Computing
![Page 5: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/5.jpg)
Architecture
External Layer
• The external model represents how data is
View
View
presented to users.
• It is made up of view tables. View tables
are "virtual"-- they do not exist in physical
storage, but appear to a user as if they did
© 2010, University of Colombo School
of Computing
![Page 6: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/6.jpg)
External Layer
Dept_Code Dep_Name Manager
SAL Sales Silva
FIN Finance Perera
Department_View
Employee_View
Emp_No Emp_Name Designation Age Dept
179 Silva Manager 27 SAL
857 Perera Accountant 34 FIN
342 Dias Programmer 26 SAL
Employee_View
View Tables© 2010, University of Colombo School
of Computing
![Page 7: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/7.jpg)
External Layer
Emp_No Emp_Name Designation Age Dept
179 Silva Manager 27 Sales
857 Perera Accountant 34 Finance
342 Dias Programmer 26 Sales
Emp_Personnel
Emp_No Emp_Name Designation DOB Dept
179 Silva Manager 12-05-74 SAL
857 Perera Accountant 01-04-67 FIN
342 Dias Programmer 25-09-74 SAL
Employee
Base Tables
View Tables
Dept_Code Dep_Name Manager
SAL Sales 179
FIN Finance 857
Department
© 2010, University of Colombo School
of Computing
![Page 8: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/8.jpg)
What Are User views?
• User views
– Derived or virtual tables that are visible to
A view is a “Virtual Table”. In SQL terminology,
view is a single table that is derived from other
tables.
– Derived or virtual tables that are visible to
users
– Do not occupy any storage space
• Base Tables
– Store actual rows of data
– Occupy a particular amount of storage space© 2010, University of Colombo School
of Computing
![Page 9: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/9.jpg)
Characteristics of User views
• Behave as if it contains actual rows of data,but in fact contains none.
• Rows are derived from base table or tablesfrom which the view is defined.from which the view is defined.
• Being virtual tables the possible updateoperations that can be applied to views arelimited. However, it does not provide anylimitations on querying a view.
© 2010, University of Colombo School
of Computing
![Page 10: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/10.jpg)
SQL and User Views
Creating a View
CREATE VIEW view-name
(list of attribute names, )
AS query
© 2010, University of Colombo School
of Computing
![Page 11: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/11.jpg)
SQL User Views
• Column names specified must have the
same number of columns derived from the
query
• Data definitions for each column are derived• Data definitions for each column are derived
from the source table
• Columns will assume corresponding column
names in the source table. Names must be
specified for calculated or identical columns.
© 2010, University of Colombo School
of Computing
![Page 12: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/12.jpg)
Specification of Views
Works_On1
FnameFname LnameLname PnamePname HoursHours
CREATE VIEW Works_On1 ASCREATE VIEW Works_On1 AS
SELECT Fname, Lname, Pname, Hours
FROM Employee, Project, Works_On
WHERE Essn = Empid
AND Pno = Pnumber ;
© 2010, University of Colombo School
of Computing
![Page 13: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/13.jpg)
Specification of Views
Dept_nameDept_name No_Of_EmpsNo_Of_Emps Total_SalTotal_Sal
Dept_Info
CREATE VIEW Dept_Info (Dept_Name, CREATE VIEW Dept_Info (Dept_Name,
No_Of_Emps, Total_Sal) AS
SELECT Dname, COUNT(*), SUM(Salary)
FROM Department, Employee
WHERE Dnumber = Dno
GROUP BY Dname ;
© 2010, University of Colombo School
of Computing
![Page 14: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/14.jpg)
Specification of Views
• Retrieve the last name and first name of all
employees who work on ‘ProjectX’.
SELECT Fname, LnameSELECT Fname, Lname
From Works_On1
WHERE Pname = ‘ProjectX’ ;
© 2010, University of Colombo School
of Computing
![Page 15: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/15.jpg)
Why User views?
Benefits
Security
Protect data from unauthorized access.
Each user is given permission to access
the database via only a small set of
views that contain specific data the user
is authorized to see.
© 2010, University of Colombo School
of Computing
![Page 16: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/16.jpg)
User views
Query Simplicity
Turning multiple table queries to single
table queries against views, by drawing data
from several tables. It provides flexible andfrom several tables. It provides flexible and
powerful data access capabilities.
© 2010, University of Colombo School
of Computing
![Page 17: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/17.jpg)
User views
Query Simplicity contd.
It also improves productivity of end-user and
programmers by:
– Simplifying database access by presenting the – Simplifying database access by presenting the
structure of data that is most natural to the user.
– Simplifying the use of routine and repetitive
statements
© 2010, University of Colombo School
of Computing
![Page 18: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/18.jpg)
User views
Natural Interface“Personalized” view of database structure, that
make sense for the user. Restructure or tailormake sense for the user. Restructure or tailor
the way in which tables are seen, so that
different users see it from different
perspectives, thus allowing more natural
views of the same enterprise (e.g. item names)
© 2010, University of Colombo School
of Computing
![Page 19: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/19.jpg)
User views
Insulation from change
Data independence - maintain independence
among different user views and between
each user view and the physical constructs.each user view and the physical constructs.
A view can present a consistent image of
the database structure, even if the
underlying source tables are restructured.
© 2010, University of Colombo School
of Computing
![Page 20: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/20.jpg)
User view Design Considerations
• User view design is driven by specific
application requirements
• User may be defined for individual user, or
a group of users, of the transaction or
application
© 2010, University of Colombo School
of Computing
![Page 21: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/21.jpg)
Design Considerations
• User view may be defined to control andrestrict access to specific columns and/or rowsin one or more tables
• User views can be defined to help simplify• User views can be defined to help simplifyqueries, application development andmaintenance
• User views may be derived from base tablesor other user views
© 2010, University of Colombo School
of Computing
![Page 22: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/22.jpg)
Remove a User View
Drop a View
DROP VIEW view-name
E.g.E.g.
DROP VIEW Emp_Payroll
Removes only the definition of the view table.
Data that it used to retrieve is not affected.
© 2010, University of Colombo School
of Computing
![Page 23: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/23.jpg)
View Implementation
• Two main approaches have been suggested for
efficiently implementing a view for querying.
• query modification
• view materialization
© 2010, University of Colombo School
of Computing
![Page 24: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/24.jpg)
Query Modification
• Involves modifying the view query into a query on
the underlying base tables. For example:
SELECT Fname, Lname
From Works_On1
WHERE Pname = ‘ProjectX’ ; WHERE Pname = ‘ProjectX’ ;
SELECT Fname, LnameSELECT Fname, Lname
From Employee, Project, Works_OnFrom Employee, Project, Works_On
WHERE Empid = Essn AND Pno = Pnumber WHERE Empid = Essn AND Pno = Pnumber
AND Pname = ‘ProjectX’ ;AND Pname = ‘ProjectX’ ;
© 2010, University of Colombo School
of Computing
![Page 25: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/25.jpg)
Query Modification
• The disadvantage of this approach is that it is
inefficient for views defined via complex
queries that are time consuming to execute.
© 2010, University of Colombo School
of Computing
![Page 26: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/26.jpg)
View Materialization
• Involves physically creating a temporary view
table when the view is first queried and
• Keeping the table on the assumption that other
queries on the view will follow.queries on the view will follow.
• An efficient strategy for automatically updating
the view table when the base tables are updated
must be developed – incremental update.
• The view is kept as long as it is being queried.
© 2010, University of Colombo School
of Computing
![Page 27: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/27.jpg)
View Update
• Updating of views is complicated and can beambiguous.
• An update on a view defined on a single table withoutany aggregate functions can be mapped to an updateany aggregate functions can be mapped to an updateon the underlying base table under certain conditions.
• For a view involving joins, an update operation maybe mapped to update operations on the underlyingbase relations in multiple ways.
© 2010, University of Colombo School
of Computing
![Page 28: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/28.jpg)
View Update
• Consider the view Works_On1 and issue command to updatethe Pname attribute of ‘Sunil Perera’ from ‘ProductX’ to‘ProductY’.
UPDATE Works_On1
SET Pname = ‘ProductY’SET Pname = ‘ProductY’
WHERE Lname = ‘Perera’AND Fname = ‘Sunil’
AND Pname = ‘ProductX’;
This query can be mapped into several updates on thebase relations to give the desired update effect on theview.
© 2010, University of Colombo School
of Computing
![Page 29: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/29.jpg)
View Update
a) UPDATE Works_On
SET Pno = ( SELECT Pnumber
FROM Project
WHERE Pname = ‘ProductY’
WHERE Essn IN (SELECT Empid
FROM EmployeeFROM Employee
WHERE Lname = ‘Perera’ AND Fname = ‘Sunil’)
AND Pno = (SELECT Pnumber
FROM Project
WHERE Pname = ‘ProductX’ ) ;
b) UPDATE Project
SET Pname = ‘ProductY’
WHERE Pname = ‘ProductX’ ;
© 2010, University of Colombo School
of Computing
![Page 30: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/30.jpg)
View Update
• A view update is feasible when only one possible
update on the base relations can accomplish the
desired update effect on the view.
• Whenever an update on the view can be mapped to• Whenever an update on the view can be mapped to
more than one update on the underlying base
relations, there must be a certain procedure for
choosing the desired update.
© 2010, University of Colombo School
of Computing
![Page 31: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/31.jpg)
With Check Option
Migrating rows
When a row is altered such that it no longer satisfies WHERE
condition then it will disappear from the view. New rows will appear
within the as a result of update or insert statement. Rows that enter
or leave a view are called migrating rows.
• WITH CHECK OPTION clause of the CREATE VIEW statement
prohibits a row migrating out of the view.
• Ensures that if a row fails to satisfy WHERE clause of
defining query, it is not added to underlying base table
31
![Page 32: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/32.jpg)
Eg:
CREATE VIEW Emp_View ASSELECT *
FROM Employee
WHERE Dept = ‘SAL’
WITH CHECK OPTION
If we now attempt to update the Department of one of the rows
from SAL to FIN for example ;
UPDATE Emp_View
SET Dept = FIN
WHERE Emp_no = 179
This would cause the row to migrate from this view. But WITH
CHECK OPTION clause in the definition of the view prevents
this from happening
32
![Page 33: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/33.jpg)
Limitations of User views
Restrictions on views processingSELECT, INSERT, UPDATE and DELETE
statements may refer to views, but there are
a number of limitations.a number of limitations.
Update may be possible for ‘simple’ views
but not ‘complex’ views.
33
![Page 34: RDBMS_IT_Lecture_View_Via_SQL](https://reader033.vdocument.in/reader033/viewer/2022060110/55620a84d8b42af2128b481f/html5/thumbnails/34.jpg)
Limitations
PerformanceDBMS must translate queries against the
view to queries against the source tables.
These disadvantages means that weThese disadvantages means that we
cannot indiscriminately define and use
views instead of source tables.
34