Views

546 views
486 views

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
546
On SlideShare
0
From Embeds
0
Number of Embeds
99
Actions
Shares
0
Downloads
21
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Views

  1. 1. VIEWS Chapter 14
  2. 2. What is a view? <ul><li>A VIRTUAL table in the database whose contents are defined by a query </li></ul><ul><li>SQL query permanently stored in the database with a name </li></ul><ul><li>To a user a view appears the same as a table </li></ul><ul><li>But unlike a real table,records of the view are not stored in the database </li></ul><ul><li>The data in the view comes from the source tables. </li></ul>
  3. 3. Creating a view <ul><li>Create view V_emp_dept as select ename , dname from emp,dept where emp.deptno=dept.deptno; </li></ul><ul><li>Once the view is defined it can be used in select statement like a table </li></ul><ul><li>Select * from V_emp_dept ; </li></ul>
  4. 4. Advantages of Views <ul><li>Security:Users to be given permission to access db through views </li></ul><ul><li>Query simplicity </li></ul><ul><li>Structural simplicity </li></ul><ul><li>Dynamic nature:definition of view unchanged when table updated </li></ul><ul><li>Data Integrity maintained </li></ul>
  5. 5. Disadvantages of Views <ul><li>Performance issues </li></ul><ul><li>Update restrictions:not always possible to update views </li></ul>
  6. 6. Types of views <ul><li>Horizontal view </li></ul><ul><li>Vertical view </li></ul><ul><li>Row/Column subset view </li></ul><ul><li>Grouped view </li></ul><ul><li>Joined view </li></ul>
  7. 7. Horizontal view <ul><li>Create view hvdept10 as select * from emp </li></ul><ul><li>where deptno=10; </li></ul><ul><li>Access to Only selected rows </li></ul><ul><li>Slices source table horizontally to create view </li></ul><ul><li>All columns of source table are part of the view </li></ul>
  8. 8. Vertical view <ul><li>Create view vvemp </li></ul><ul><li>as select ename,sal,comm from emp; </li></ul><ul><li>Access to Only selected columns </li></ul><ul><li>Slices source table vertically to create view </li></ul><ul><li>All rows of source table are part of the view </li></ul>
  9. 9. Row/Column subset view <ul><li>Create view rcemp as select ename,sal,comm from emp </li></ul><ul><li>where deptno=10; </li></ul><ul><li>Access to selected columns and rows </li></ul><ul><li>Only the columns named in select list and rows that meet search condition form part of the view </li></ul>
  10. 10. Grouped View <ul><li>Create view gvemp(department,no_emp,total_sal, avg_sal,max_sal,min_sal) </li></ul><ul><li>as select deptno,count(*),sum(sal), </li></ul><ul><li>avg(sal),max(sal),min(sal) </li></ul><ul><li>from emp </li></ul><ul><li>group by deptno; </li></ul><ul><li>Select * from gvemp; </li></ul>
  11. 11. Grouped View(contd.) <ul><li>Select query has a group by clause </li></ul><ul><li>Produce one row for each group </li></ul><ul><li>Definition always includes a list of column names </li></ul><ul><li>Do not have one to one correspondence with source table rows </li></ul><ul><li>Summary of source table/tables </li></ul><ul><li>Cannot be updated </li></ul>
  12. 12. Joined View <ul><li>Create view V_emp_dept as select ename , dname from emp,dept where emp.deptno=dept.deptno; </li></ul><ul><li>Draws data from two or more tables </li></ul><ul><li>For the user ,querying becomes easy </li></ul>
  13. 13. Updating Views <ul><li>Whenever updates are made through the view the source table data is also updated </li></ul><ul><li>Views may be updateable or non updateable </li></ul><ul><li>Generally views made with simple queries are updateable </li></ul>
  14. 14. Restrictions on updating views <ul><li>No distinct used in select </li></ul><ul><li>From clause must have one source table </li></ul><ul><li>Each select item a simple column name </li></ul><ul><li>Where clause should not have a sub query </li></ul><ul><li>No group by or having clause </li></ul>
  15. 15. With Check Option <ul><li>To prevent updation of rows which are not visible through the view WITH CHECK OPTION is used </li></ul><ul><li>SQL automatically checks each Insert and update operation for the view to make sure, resulting row meets search condition in view definition </li></ul>
  16. 16. With Check Option(Example) <ul><li>create view rcemp </li></ul><ul><li>as select ename,sal,comm </li></ul><ul><li>from emp where sal>1000 </li></ul><ul><li>with check option; </li></ul><ul><li>View created </li></ul><ul><li>update rcemp set sal=800 where ename='ALLEN'; </li></ul><ul><li>ORA-01402: view WITH CHECK OPTION where-clause violation </li></ul>
  17. 17. Dropping a view <ul><li>Drop view <view_name>; </li></ul><ul><li>Only view dropped not the source tables </li></ul>
  18. 18. Materialized views <ul><li>When the query defining a view is complex the dbms materializes the view </li></ul><ul><li>Saves results of query temporarily in a table </li></ul><ul><li>Discards the table when query is over </li></ul><ul><li>Materialized view -query run once,data saved permanently,with every update to source table corresponding changes to view </li></ul>
  19. 19. THANKS

×