32
DATABASE SYSTEMS Views in Oracle 10g Version 1.0 Rushdi Shams, Dept of CSE, KUET 1

L6 views

  • View
    232

  • Download
    3

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: L6  views

DATABASE SYSTEMS

Views in Oracle 10gVersion 1.0

Rushdi Shams, Dept of CSE, KUET 1

Page 2: L6  views

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.

Page 3: L6  views

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!

Page 4: L6  views

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!

Page 5: L6  views

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.

Page 6: L6  views

Rushdi Shams, Dept of CSE, KUET 6

Our Table

This is our table. We will play with this table to understand views.

Page 7: L6  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

Page 8: L6  views

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.

Page 9: L6  views

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.

Page 10: L6  views

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?

Page 11: L6  views

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.

Page 12: L6  views

Rushdi Shams, Dept of CSE, KUET 12

Querying the Second View

Again we have queried the second view and found appropriate records.

Page 13: L6  views

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.

Page 14: L6  views

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.

Page 15: L6  views

Rushdi Shams, Dept of CSE, KUET 15

Querying the View

Page 16: L6  views

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.

Page 17: L6  views

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?

Page 18: L6  views

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.

Page 19: L6  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.

Page 20: L6  views

Rushdi Shams, Dept of CSE, KUET 20

Querying the View

Page 21: L6  views

Rushdi Shams, Dept of CSE, KUET 21

Our View 1 and View 4

Page 22: L6  views

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.

Page 23: L6  views

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.

Page 24: L6  views

Rushdi Shams, Dept of CSE, KUET 24

To make a View Updatable

Page 25: L6  views

Rushdi Shams, Dept of CSE, KUET 25

Updating through an Updatable View

Page 26: L6  views

Rushdi Shams, Dept of CSE, KUET 26

Querying the Updated View

Page 27: L6  views

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.

Page 28: L6  views

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)

Page 29: L6  views

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.

Page 30: L6  views

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

Page 31: L6  views

Rushdi Shams, Dept of CSE, KUET 31

Dropping the Views

Finally, we are dropping all the four views with DROP VIEW option.

Page 32: L6  views

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]