You are on page 1of 7

1-What is view?

2-Benefits of view?
3-Can we alter a view?
4-Where view is stored & why query fired on a view will run slower than a query fired on a base table?
5-When view is updateable?
6-Inline view & materialized view.

1- View –

The concept of view is very simple: Define a query that is going to be used frequently, store
it in the oracle database, and allow users to call it by name, as would a table.

 A view is a logical table based on a table or another view.


 A view contains no data of its own but is like a window through which data from
tables can be viewed or changed. The view is stored as a SELECT statement in the
data dictionary.
 The table on which a view is based are called base tables.

How can we create a view on a non existing table


CREATE OR REPLACE VIEW view_name as subquery ( select * from tablename);

2- Why are views useful?

1- To make complex queries easy


One very common use for a view is to join data from two or more tables and present it to
users in one easy-to-read list. By simplifying the record-retrieval process so that users don’t
have to understand how to join tables

2- To restrict data access


Views are also useful for enforcing security, because they allow you to limit the columns and
rows returned to the user.

3- Alter view definition


Oracle does not provide a way to alter an existing view. The only way to change a view is to
drop it & re-create it.
For this reason, it’s a good idea to store all of your view-creation commands in .sql
script files. That way, if you need to change a view, it’s easy to open the script file, change
the command it contains, and rerun the CREATE VIEW command.
4- An interesting fact about a view is that it is stored only as a definition in Oracle’s system
catalog.

.
A query fired on a view will run slower than a query fired on a base table. This is because
the view definition has to be retrieved from oracle’s system catalog, the base table has to be
identified and opened in memory and then the view has to be constructed on top of the
base table, suitably masking table columns. Only then will query actually execute and return
the active data set.

5-Some views are used only for looking at table data. Other views can be used to Insert
Update and Delete table data as well as view data.
If a view is used to only look at table data and nothing else the view is call a Read-only
view.
A view that is used to look at table data as well as Insert, Update and delete table data is
called an updateable view.

Note- The ORDER BY clause cannot be used while creating a view.


The SELECT statement can have all the clauses like WHERE, ORDER BY etc.

For a view to be updateable (DML operations), it should meet the following criteria:

 Views defined from single table.


 If the user wants to INSERT records with the help of 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.

If a view is created from multiple tables, which were not created using a Reference clause
(Logical linkage exists between the tables), even though the PRIMARY KEY column(s) as
well as the NOT NULL columns are included in the view definition the view’s behaviour will
be as follows:
The INSERT, UPDATE or DELETE operation is not allowed.

You cannot remove a row if the view contains the following:


 Group functions- max,min,avg,sum,count
 A Group by clause
 The distinct keyword
 The pseudo column ROWNUM keyword
An inline view is a SELECT statement in the FROM clause. 
An inline view is not a real view but a subquery in the FROM clause of a SELECT statement.

 In an inline view construct, instead of specifying table name(s) after the FROM keyword, the source of the
data actually comes from the inline view.

Syntax- The syntax for an inline view is,


SELECT "column_name" FROM (Inline View);

Consider the following SELECT statement:

SELECT   column_list FROM  table;

In the FROM clause, you can specify a table from which you want to query data. Besides a table, you
can use a subquery as shown in the following example:

SELECT  column_list FROM  (   SELECT   *    FROM  table_name  ) t;

The subquery specified in the FROM clause of a query is called an inline view.


Because an inline view can replace a table in a query, it is also called a derived table.( Inline view is
sometimes referred to as derived table. These two terms are used interchangeably.)
Oracle inline view example
Let’s use the  products  table in the sample database for the demonstration.

A) simple Oracle inline view example


The following query retrieves the top 10 most expensive products from
the products table:
1 SELECT
2     *
3 FROM
4     (
5         SELECT
6             product_id,
7             product_name,
8             list_price
9         FROM
10             products
11         ORDER BY
12             list_price DESC
13     )
14 WHERE
15     ROWNUM <= 10;
In this example, first, the inline view returns all products sorted by list prices in
descending order. And then the outer query retrieves the first 10 rows from the
inline view.

B) Inline view joins with a table example


The following example joins an inline view with a table in the FROM clause. It
returns the product categories and the highest list price of products in each
category:

1 SELECT
2     category_name,
3     max_list_price
4 FROM
5     product_categories a,
6     (
7         SELECT
8             category_id,
9             MAX( list_price ) max_list_price
10         FROM
11             products
12         GROUP BY
13             category_id
14     ) b
15 WHERE
16     a.category_id = b.category_id
17 ORDER BY
18     category_name;

In this example, the inline view returns the category id list and the highest list
price of product in each category. The outer query joins the inline view with
the  product_categories  table to get the category name.
What is a Materialized View?

Materialised view is the view that is physically present in the database.


 The materialized view is a table whose contents are periodically refreshed using a query against a
remote table.

When a materialized view is fast refreshed, Oracle must examine all of the changes to the master
table or master materialized view since the last refresh to see if any apply to the materialized
view.

Therefore, if any changes where made to the master since the last refresh, then a materialized view
refresh takes some time to apply the changes to the materialized view. If, however, no changes at all
were made to the master since the last refresh of a materialized view, then the materialized view
refresh should be very quick.

Read-Only Materialized Views

You can make a materialized view read-only during creation by omitting the FOR UPDATE clause

In addition, using read-only materialized views eliminates the possibility of a materialized view
introducing data conflicts at the master site or master materialized view site, although this
convenience means that updates cannot be made at the remote materialized view site. The
following is an example of a read-only materialized view:

CREATE MATERIALIZED VIEW hr.employees AS


SELECT * FROM hr.employees@orc1.world;

Updatable Materialized Views

You can make a materialized view updatable during creation by including the FOR UPDATE clause.

For changes made to an updatable materialized view to be pushed back to the master during
refresh, the updatable materialized view must belong to a materialized view group.

Updatable materialized views enable you to decrease the load on master sites because users can
make changes to the data at the materialized view site. The following is an example of an updatable
materialized view:

CREATE MATERIALIZED VIEW hr.departments FOR UPDATE AS


SELECT * FROM hr.departments@orc1.world;
28.  Explain materialized views and how they are used.

Materialized views are objects that are reduced sets of information that have been
summarized, grouped, or aggregated from base tables. They are typically used in data
warehouse or decision support systems.

30.  What background process refreshes materialized views?

The Job Queue Processes.

You might also like