L6 views

236 views
177 views

Published on

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

  • Be the first to like this

No Downloads
Views
Total views
236
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

L6 views

  1. 1. Views in Oracle 10g Version 1.0 Rushdi Shams, Dept of CSE, KUET 1
  2. 2. What is a View  View is a virtual table.  It does not contain any data.  Views, however, are saved in the database just like tables.  So, views are database objects.  A view is simply a window to a database and represents a particular perspective to the user. Rushdi Shams, Dept of CSE, KUET 2
  3. 3. What is a View  Views are similar to queries in that respect that they too derive a result table – which is virtual from the actual base table.  The difference between views and queries is – views can be queried as well! Rushdi Shams, Dept of CSE, KUET 3
  4. 4. Advantage of View  I said – Views are windows to the database tables.  Windows are used to see the outside world.  So, is view used to only see the outside world? Not at all! Rushdi Shams, Dept of CSE, KUET 4
  5. 5. Advantage of View  The main point of view is to restrict what you can see in the underlying database table by selective columns.  They can be used to make complex queries easy. A user can use a simple query on a view to display data from multiple tables, without having the knowledge of how to join tables in queries.  Different views can be created from the same data as per the requirements of different types of use groups. Rushdi Shams, Dept of CSE, KUET 5
  6. 6. Our Table Rushdi Shams, Dept of CSE, KUET 6  This is our table. We will play with this table to understand views.
  7. 7. Creating a View Rushdi Shams, Dept of CSE, KUET 7  We are now creating a view named test_view that selects all records of CSD students
  8. 8. Querying the View Rushdi Shams, Dept of CSE, KUET 8  As we said earlier, we can query a view, we have selected everything test_view provides.
  9. 9. Querying the View Rushdi Shams, Dept of CSE, KUET 9  In this case, we have filtered the result found from the view with WHERE clause.
  10. 10. View makes Complex Query Simple Rushdi Shams, Dept of CSE, KUET 10  In SQL, the query we have made in the previous slide should look like this.  Which one is simpler?
  11. 11. Creating another View Rushdi Shams, Dept of CSE, KUET 11  This time we are creating another view named test_view_2 which will contain the records of BSD students.
  12. 12. Querying the Second View Rushdi Shams, Dept of CSE, KUET 12  Again we have queried the second view and found appropriate records.
  13. 13. Simplicity  This property is called simplicity  The student data are for the whole university  We have simplified the data so that BSD people won’t have to bother about CSD people and vice versa. Rushdi Shams, Dept of CSE, KUET 13
  14. 14. Creating the Third View Rushdi Shams, Dept of CSE, KUET 14  test_view_3 is created.  The third line takes the months between the current date (sysdate) and the date on birthday column. Then divides it by 12 to get the year. Then the whole number is passed as argument of FLOOR function. That is the age of every entry in the table.
  15. 15. Querying the View Rushdi Shams, Dept of CSE, KUET 15
  16. 16. Functionality  Another property of views.  It means that we can provide the user AGE and the user may never ever be able to know that there is actually no column such as AGE. AGE is actually derived from other columns of the table. Rushdi Shams, Dept of CSE, KUET 16
  17. 17. Security  It is the third property of views.  It is an extension of SIMPLICITY property.  BSD student records are safe now from CSD students (As CSD students are technologically smart )  Do you think teachers will allow you to see their salaries? Rushdi Shams, Dept of CSE, KUET 17
  18. 18. Updating through Views  Now we will take a look into issues we need to know while updating tables through views. Rushdi Shams, Dept of CSE, KUET 18
  19. 19. Creating Fourth View Rushdi Shams, Dept of CSE, KUET 19  We have created test_view_4 which does not have all the columns of the table.
  20. 20. Querying the View Rushdi Shams, Dept of CSE, KUET 20
  21. 21. Our View 1 and View 4 Rushdi Shams, Dept of CSE, KUET 21
  22. 22. What We can do View 1? Rushdi Shams, Dept of CSE, KUET 22  we can insert a new row into the view  delete an existing record from a view  update an existing field in the view.
  23. 23. Problem updating through View 4  suppose we wished to insert a record for a new student, F, M, BSD. We would have to insert the record null, F, M, null, null, BSD into the underlying students table.  This attempt will fail since studentNo (the table’s primary key) must not be null. 23Rushdi Shams, Dept of CSE, KUET
  24. 24. To make a View Updatable Rushdi Shams, Dept of CSE, KUET 24
  25. 25. Updating through an Updatable View Rushdi Shams, Dept of CSE, KUET 25
  26. 26. Querying the Updated View Rushdi Shams, Dept of CSE, KUET 26
  27. 27. Inserting through a Wrong View Rushdi Shams, Dept of CSE, KUET 27  Now, we are trying to enter a student of BSD through a view (view 1) created for CSD students.
  28. 28. The Result! Rushdi Shams, Dept of CSE, KUET 28  The view inserted it into the table but it cannot be queried with view 1  You can query it through view 2 (created for BSD students)
  29. 29. The Solution Rushdi Shams, Dept of CSE, KUET 29  We have changed the CREATE VIEW statement for our view 1 by adding WITH CHECK OPTION  Now we are attempting to insert records for BSD students through a view created for CSD students.
  30. 30. The Result Rushdi Shams, Dept of CSE, KUET 30  Oracle got you!  You cannot violate that when you have created the view with a WITH CHECK OPTION
  31. 31. Dropping the Views Rushdi Shams, Dept of CSE, KUET 31  Finally, we are dropping all the four views with DROP VIEW option.
  32. 32. References  Database Systems (Third Edition) by Paul Beynon-Davies  Structured Query Language (SQL): A Practical Introduction by Akil I. Din  Oracle forums at http://forums.oracle.com/forums/thread.j spa?threadID=378783 [21 March 2008]  Geek Interview at http://www.geekinterview.com/question_ details/338 [21 March 2008] Rushdi Shams, Dept of CSE, KUET 32

×