Oracle view

madhavendradutt 128 views 18 slides Jan 20, 2019
Slide 1
Slide 1 of 18
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18

About This Presentation

Learn about what is a view in database, how to create and use views in oracle..


Slide Content

View

WHAT IS VIEW
A View in Oracle and in other database systems is simply
the representation of a SQL statement that is stored in
memory so that it can easily be re-used.
It prevents all users from accessing all columns of a table.
It reduces redundant data to the minimum possible.

It is stored only as a definition in Oracle’s system
catalogue. When a reference is made to a View, its
definition is scanned, the base table is opened and the
View is created on the top of the table.
Read-only View is used only for looking at table data.
Updateable View can be used to look at table data as
well as Insert, Update and Delete table data.

WHY VIEW
When data security is required.
When Data redundancy is to be kept to the minimum while
maintaining data security

PRACTICAL
Create
Read-only and Updatable view
Destroy

Prerequisites
To create a view in your own schema, you must have the CREATE
VIEW system privilege. To create a view in another user's schema,
you must have the CREATE ANY VIEW system privilege.
To create a subview, you must have the UNDER ANY VIEW
system privilege or the UNDER object privilege on the superview.
The owner of the schema containing the view must have the
privileges necessary to either select, insert, update, or delete rows
from all the tables or views on which the view is based. The owner
must be granted these privileges directly, rather than through a
role.

Syntax
CREATE VIEW viewname AS
SELECT columnname, columnname
FROM tablename
WHERE columnname=expression list
GROUP BY grouping criteria
HAVING predicate

Examples
CREATE VIEW sales_view AS
SELECT *
FROM salesman_master
WITH READ ONLY;
CREATE VIEW emp_view AS
SELECT last_name, salary*12 annual_salary
FROM employees
WHERE department_id = 20;

Updateable View
For a view to be updateable, it should meet the following criteria:
1.Views defined from Single table:
If the user wants to INSERT records with the help of a view, then the
PRIMARY KEY column/s and all the NOT NULL columns must be
included in the view.
The user can UPDATE, DELETE records with the help of a view
even if the PRIMARY KEY column and NOT NULL column/s are
excluded from the view definition.

CREATE VIEW vw_client AS
SELECT cient_no, name, address1, bal_due
FROM client_master;

2.Views defined from Multiple tables (Which have no
Referencing clause)
If a view is created from multiple tables, which were not created
using a ‘Referencing clause’, then though the PRIMARY KEY
column/s as well as the NOT NULL columns are included in the
View definition the view’s behavior will be as follows:
The INSERT, UPDATE or DELETE operation is not allowed.

3.Views defined from Multiple tables (Which have been
created with a Referencing clause)
If a view is created from multiple tables, which were created
using a ‘Referencing clause’, then though the PRIMARY KEY
column/s as well as NOT NULL columns are included in the
View definition the view’s behavior will be as follows:
An INSERT operation is not allowed.
The DELETE or MODIFY operations do not affect the Master table.
The view can be used to MODIFY the columns of the detail table
included in the view.
If a DELETE operation is executed on the view, the corresponding
records from the detail table will be deleted.

CREATE VIEW locations_view AS
SELECT d.department_id, d.department_name, l.location_id,
l.city
FROM departments d, locations l
WHERE d.location_id = l.location_id;

ALTER VIEW
Use the ALTER VIEW statement to explicitly recompile a
view that is invalid or to modify view constraints. Explicit
recompilation lets you locate recompilation errors before
run time. You may want to recompile a view explicitly after
altering one of its base tables to ensure that the alteration
does not affect the view or other objects that depend on it.

You can also use ALTER VIEW to define, modify, or drop view
constraints.
This statement does not change the definition of an existing
view. To redefine a view, you must use CREATE VIEW with
the OR REPLACE keywords.
When you issue an ALTER VIEW statement, Oracle Database
recompiles the view regardless of whether it is valid or invalid.
The database also invalidates any local objects that depend on the
view.

ALTER VIEW customer_ro COMPILE;
CREATE or REPLACE VIEW vw_client AS
SELECT cient_no, name, bal_due
FROM client_master;

Extra
To see details of a view, use describe command
DESC my_view;
To see all views created by user
SELECT object_name, object_type, created FROM
user_objects WHERE object_type=‘VIEW’;

To see the source code of the view
SELECT view_name, text FROM user_views WHERE
view_name=‘my_created_view’;
It displays only 80 characters text of view. It is because the display
length of “LONG” data type is 80 and the “text” column of
“user_views” is of type “LONG”. We can increase the display
length as follows:
SET long 200;