View
232
Download
3
Embed Size (px)
DESCRIPTION
Citation preview
DATABASE SYSTEMS
Views in Oracle 10gVersion 1.0
Rushdi Shams, Dept of CSE, KUET 1
Rushdi Shams, Dept of CSE, KUET 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 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 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 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 6
Our Table
This is our table. We will play with this table to understand views.
Rushdi Shams, Dept of CSE, KUET 7
Creating a View
We are now creating a view named test_view that selects all records of CSD students
Rushdi Shams, Dept of CSE, KUET 8
Querying the View
As we said earlier, we can query a view, we have selected everything test_view provides.
Rushdi Shams, Dept of CSE, KUET 9
Querying the View
In this case, we have filtered the result found from the view with WHERE clause.
Rushdi Shams, Dept of CSE, KUET 10
View makes Complex Query Simple
In SQL, the query we have made in the previous slide should look like this.
Which one is simpler?
Rushdi Shams, Dept of CSE, KUET 11
Creating another View
This time we are creating another view named test_view_2 which will contain the records of BSD students.
Rushdi Shams, Dept of CSE, KUET 12
Querying the Second View
Again we have queried the second view and found appropriate records.
Rushdi Shams, Dept of CSE, KUET 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 14
Creating the Third View
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.
Rushdi Shams, Dept of CSE, KUET 15
Querying the View
Rushdi Shams, Dept of CSE, KUET 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 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 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 19
Creating Fourth View
We have created test_view_4 which does not have all the columns of the table.
Rushdi Shams, Dept of CSE, KUET 20
Querying the View
Rushdi Shams, Dept of CSE, KUET 21
Our View 1 and View 4
Rushdi Shams, Dept of CSE, KUET 22
What We can do View 1?
we can insert a new row into the view delete an existing record from a view update an existing field in the view.
23Rushdi Shams, Dept of CSE, KUET
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.
Rushdi Shams, Dept of CSE, KUET 24
To make a View Updatable
Rushdi Shams, Dept of CSE, KUET 25
Updating through an Updatable View
Rushdi Shams, Dept of CSE, KUET 26
Querying the Updated View
Rushdi Shams, Dept of CSE, KUET 27
Inserting through a Wrong View
Now, we are trying to enter a student of BSD through a view (view 1) created for CSD students.
Rushdi Shams, Dept of CSE, KUET 28
The Result!
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)
Rushdi Shams, Dept of CSE, KUET 29
The Solution
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.
Rushdi Shams, Dept of CSE, KUET 30
The Result
Oracle got you! You cannot violate that when you have
created the view with a WITH CHECK OPTION
Rushdi Shams, Dept of CSE, KUET 31
Dropping the Views
Finally, we are dropping all the four views with DROP VIEW option.
Rushdi Shams, Dept of CSE, KUET 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.jspa?threadID=378783 [21 March 2008]
Geek Interview at
http://www.geekinterview.com/question_details/338 [21 March 2008]