Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Database Management      Systems       Views Dr. Jeevani Goonetillake          UCSC     © 2010, University of Colombo Scho...
External LayerView          View               View                                   Conceptual Layer               Base ...
Table                                              Architecture                                        Conceptual Layer   ...
Architecture Conceptual Layer                 Department                 Dept_Code Dep_Name Manager                 SAL   ...
Architecture                     View                                            External Layer                     View• ...
External Layer                    Department_View                      Dept_Code Dep_Name Manager                      SAL...
Emp_Personnel                                                         External Layer        Emp_No Emp_Name Designation Ag...
What Are User views? A view is a “Virtual Table”. In SQL terminology, view is a single table that is derived from other  t...
Characteristics of User views• Behave as if it contains actual rows of data,  but in fact contains none.• Rows are derived...
SQL and User ViewsCreating a View       CREATE VIEW view-name         (list of attribute names, )         AS query        ...
SQL User Views• Column names specified must have the  same number of columns derived from the  query• Data definitions for...
Specification of ViewsWorks_On1    Fname Lname Pname Hours  CREATE VIEW Works_On1 AS  SELECT Fname, Lname, Pname, Hours  F...
Specification of ViewsDept_Info  Dept_name No_Of_Emps Total_Sal  CREATE VIEW Dept_Info (Dept_Name,          No_Of_Emps, To...
Specification of Views• Retrieve the last name and first name of all  employees who work on ‘ProjectX’.     SELECT Fname, ...
Why User views?Benefits  Security     Protect data from unauthorized access.     Each user is given permission to access  ...
User viewsQuery Simplicity   Turning multiple table queries to single   table queries against views, by drawing data   fro...
User viewsQuery Simplicity contd.  It also improves productivity of end-user and     programmers by:   – Simplifying datab...
User viewsNatural Interface   “Personalized” view of database structure, that   make sense for the user. Restructure or ta...
User viewsInsulation from change  Data independence - maintain independence  among different user views and between  each ...
User view Design Considerations• User view design is driven by specific  application requirements• User may be defined for...
Design Considerations• User view may be defined to control and  restrict access to specific columns and/or rows  in one or...
Remove a User ViewDrop a View        DROP VIEW view-nameE.g.       DROP VIEW Emp_PayrollRemoves only the definition of the...
View Implementation• Two main approaches have been suggested for  efficiently implementing a view for querying.     • quer...
Query Modification• Involves modifying the view query into a query on  the underlying base tables. For example:           ...
Query Modification• The disadvantage of this approach is that it is  inefficient for views defined via complex  queries th...
View Materialization• Involves physically creating a temporary view  table when the view is first queried and• Keeping the...
View Update• Updating of views is complicated and can be  ambiguous.• An update on a view defined on a single table withou...
View Update• Consider the view Works_On1 and issue command to update  the Pname attribute of ‘Sunil Perera’ from ‘ProductX...
View Updatea)  UPDATE Works_On   SET Pno = ( SELECT Pnumber                    FROM Project                    WHERE Pname...
View Update• A view update is feasible when only one possible  update on the base relations can accomplish the  desired up...
With Check OptionMigrating rows  When a row is altered such that it no longer satisfies WHERE  condition then it will disa...
Eg:CREATE VIEW Emp_View ASSELECT    *FROM      EmployeeWHERE     Dept = ‘SAL’WITH CHECK OPTIONIf we now attempt to update ...
Limitations of User viewsRestrictions on views processing   SELECT, INSERT, UPDATE and DELETE   statements may refer to vi...
LimitationsPerformance   DBMS must translate queries against the   view to queries against the source tables.   These disa...
Upcoming SlideShare
Loading in …5
×

RDBMS_IT_Lecture_View_Via_SQL

436 views

Published on

DBMS_VIEW_ Contain SQL

Published in: Education
  • Be the first to comment

RDBMS_IT_Lecture_View_Via_SQL

  1. 1. Database Management Systems Views Dr. Jeevani Goonetillake UCSC © 2010, University of Colombo School of Computing
  2. 2. External LayerView View View Conceptual Layer Base Tables Physical Layer Stored Tables ... © 2010, University of Colombo School of Computing
  3. 3. Table Architecture Conceptual Layer Table• The conceptual model is a logical representation of the entire contents of the 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
  4. 4. Architecture Conceptual Layer Department Dept_Code Dep_Name Manager SAL Sales 179 FIN Finance 857EmployeeEmp_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 Base Tables © 2010, University of Colombo School of Computing
  5. 5. Architecture View External Layer View• The external model represents how data is 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
  6. 6. External Layer Department_View Dept_Code Dep_Name Manager SAL Sales Silva FIN Finance PereraEmployee_View Emp_No Emp_Name Designation Age Dept 179 Silva Manager 27 SAL 857 Perera Accountant 34 FIN 342 Dias Programmer 26 SAL © 2010, University of Colombo School of Computing View Tables
  7. 7. Emp_Personnel External Layer Emp_No Emp_Name Designation Age Dept 179 Silva Manager 27 Sales 857 Perera Accountant 34 Finance 342 Dias Programmer 26 Sales View Tables Department EmployeeDept_Code Dep_Name Manager Emp_No Emp_Name Designation DOB Dept 179 Silva Manager 12-05-74 SALSAL Sales 179 857 Perera Accountant 01-04-67 FINFIN Finance 857 342 Dias Programmer 25-09-74 SAL Base Tables © 2010, University of Colombo School of Computing
  8. 8. What Are User views? A view is a “Virtual Table”. In SQL terminology, view is a single table that is derived from other tables.• User views – 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
  9. 9. 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 tables from which the view is defined.• Being virtual tables the possible update operations that can be applied to views are limited. However, it does not provide any limitations on querying a view. © 2010, University of Colombo School of Computing
  10. 10. SQL and User ViewsCreating a View CREATE VIEW view-name (list of attribute names, ) AS query © 2010, University of Colombo School of Computing
  11. 11. SQL User Views• Column names specified must have the same number of columns derived from the query• 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
  12. 12. Specification of ViewsWorks_On1 Fname Lname Pname Hours CREATE 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
  13. 13. Specification of ViewsDept_Info Dept_name No_Of_Emps Total_Sal 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
  14. 14. Specification of Views• Retrieve the last name and first name of all employees who work on ‘ProjectX’. SELECT Fname, Lname From Works_On1 WHERE Pname = ‘ProjectX’ ; © 2010, University of Colombo School of Computing
  15. 15. 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
  16. 16. User viewsQuery Simplicity Turning multiple table queries to single table queries against views, by drawing data from several tables. It provides flexible and powerful data access capabilities. © 2010, University of Colombo School of Computing
  17. 17. User viewsQuery Simplicity contd. It also improves productivity of end-user and programmers by: – 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
  18. 18. User viewsNatural Interface “Personalized” view of database structure, that make 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
  19. 19. User viewsInsulation from change Data independence - maintain independence among different user views and between 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
  20. 20. 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
  21. 21. Design Considerations• User view may be defined to control and restrict access to specific columns and/or rows in one or more tables• User views can be defined to help simplify queries, application development and maintenance• User views may be derived from base tables or other user views © 2010, University of Colombo School of Computing
  22. 22. Remove a User ViewDrop a View DROP VIEW view-nameE.g. DROP VIEW Emp_PayrollRemoves only the definition of the view table.Data that it used to retrieve is not affected. © 2010, University of Colombo School of Computing
  23. 23. 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
  24. 24. 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’ ; SELECT Fname, Lname From Employee, Project, Works_On WHERE Empid = Essn AND Pno = Pnumber AND Pname = ‘ProjectX’ ; © 2010, University of Colombo School of Computing
  25. 25. 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
  26. 26. 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.• 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
  27. 27. View Update• Updating of views is complicated and can be ambiguous.• An update on a view defined on a single table without any aggregate functions can be mapped to an update on the underlying base table under certain conditions.• For a view involving joins, an update operation may be mapped to update operations on the underlying base relations in multiple ways. © 2010, University of Colombo School of Computing
  28. 28. View Update• Consider the view Works_On1 and issue command to update the Pname attribute of ‘Sunil Perera’ from ‘ProductX’ to ‘ProductY’. UPDATE Works_On1 SET Pname = ‘ProductY’ WHERE Lname = ‘Perera’ AND Fname = ‘Sunil’ AND Pname = ‘ProductX’; This query can be mapped into several updates on the base relations to give the desired update effect on the view. © 2010, University of Colombo School of Computing
  29. 29. View Updatea) UPDATE Works_On SET Pno = ( SELECT Pnumber FROM Project WHERE Pname = ‘ProductY’ WHERE Essn IN (SELECT Empid FROM 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
  30. 30. 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 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
  31. 31. With Check OptionMigrating 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
  32. 32. Eg:CREATE VIEW Emp_View ASSELECT *FROM EmployeeWHERE Dept = ‘SAL’WITH CHECK OPTIONIf we now attempt to update the Department of one of the rowsfrom SAL to FIN for example ;UPDATE Emp_ViewSET Dept = FINWHERE Emp_no = 179This would cause the row to migrate from this view. But WITHCHECK OPTION clause in the definition of the view preventsthis from happening 32
  33. 33. Limitations of User viewsRestrictions on views processing SELECT, INSERT, UPDATE and DELETE statements may refer to views, but there are a number of limitations. Update may be possible for ‘simple’ views but not ‘complex’ views. 33
  34. 34. LimitationsPerformance DBMS must translate queries against the view to queries against the source tables. These disadvantages means that we cannot indiscriminately define and use views instead of source tables. 34

×