Views


A view is a logical representation of another table or combination of tables
A view is simply the representation of a sql statement that is stored in memory so that it can easily be re-used.
With some restrictions, rows can be inserted into, updated in, or deleted from a base table using a view.
Advantages of views
·         Views restrict accesses to the data because the view can display selected columns from the table.
·         Views can be used to make simple queries to retrieve the results of complicated queries for ex, views can be used to query information from multiple table without the user knowing how to write join statement.
·         Views provide groups of users access to data according to their particular criteria.

Two types of views:
1.      Simple view
2.      Complex view
If the table is dropped, if the view is queried then error message will be displayed
If the table rows are deleted, then if view is queried for the data then data present at that instant in the table will be displayed
Complex view:
Sql> create view dept_sum (name, minsal, maxsal, avgsal)
  2  as select d.dname,min(e.sal),max(e.sal),avg(e.sal) from emp e join dept d
  3  on (e.deptno=d.deptno) group by d.dname;
Sql> select * from dept_sum;
Name   minsal              maxsal             avgsal
--------------       ----------            ----------           ----------
Programmer     1300                5000                2916.66667
Research 800               3000                2175
Sales          950            2850                1566.66667
We cannot remove a row if the view contains the following
·         Group function
·         A group by clause
·         A distinct  keyword
·         The pseudo column rownum keyword
Difference between simple views and complex views
  • A simple view selects from one table. A complex view selects from one or more tables.
  • A simple view does not contain functions but complex views contain functions.
  • You can perform dml through simple views but you cannot always perform dml through complex views.

Read-only views
Users can only run select and desc statements against read only views. Examples:
Read only clause on a simple view:
Create view clerk (id_number, person, department, position)
    as select empno, ename, deptno, job
         from emp
         where job = 'clerk'
  with read only;

With check option

The with check option clause specifies the level of checking to be done when doing dml against the view. If specified, every row that is inserted, updated or deleted through the view must conform to the definition of the view.
Sql> create view d20 as select ename, sal, deptno from emp2 where deptno = 20
  2  with check option;
View created.
Sql> update d20 set deptno = 10;
Update d20 set deptno = 10
       *
Error at line 1:
Ora-01402: view with check option where-clause violation



Materialized views in oracle
A materialized view is a stored summary containing pre computes results (originating from an sql select statement).
As the data is pre computed, materialized views allow for (seemingly) faster data ware query answers
Types of materialized views
There are three types of materialized views:
  • Read only materialized view
  • Updateable materialized view
  • Writeable materialized view
Read only materialized views
Advantages:
  • There is no possibility for conflicts as they cannot be updated.
  • Complex materialized views are supported
Updateable materialized views
Advantages:
  • Can be updated even when disconnected from the master site or master materialized view site.
  • Requires fewer resources than multi master replication.
  • Are refreshed on demand. Hence the load on the network might be reduced compared to using multi master replication because multi master replication synchronizes changes at regular intervals.
Updateable materialized views require the advanced replication option to be installed.
Writeable materialized views
They are created with the for update clause during creation without then adding the materialized view to a materialized view group. In such a case, the materialized view is updatable, but the changes are lost when the materialized view refreshes.
Writeable materialized views require the advanced replication option to be installed.


A materialized view is a database object that contains the results of a query. They are local copies of data located remotely, or are used to create summary tables based on aggregations of a table's data. Materialized views, which store data based on remote tables are also, know as snapshots.
A materialized view can query tables, views, and other materialized views. Collectively these are called master tables (a replication term) or detail tables (a data warehouse term).
For replication purposes, materialized views allow you to maintain copies of remote data on your local node. These copies are read-only. If you want to update the local copies, you have to use the advanced replication feature. You can select data from a materialized view as you would from a table or view.
For data warehousing purposes, the materialized views commonly created are aggregate views, single-table aggregate views, and join views.
In replication environments, the materialized views commonly created are primary key, rowid, and subquery materialized views.
Primary key materialized views
The following statement creates the primary-key materialized view on the table emp located on a remote database.
Sql>        create materialized view mv_emp_pk
               refresh fast start with sysdate 
               next  sysdate + 1/48
               with primary key 
               as select * from emp;
 
Materialized view created.
Note: when you create a materialized view using the fast option you will need to create a view log on the master tables(s) as shown below:
Sql> create materialized view log on emp;
Materialized view log created.
Rowid materialized views
The following statement creates the rowid materialized view on table emp located on a remote database:
Sql>        create materialized view mv_emp_rowid 
               refresh with rowid 
               as select * from emp;  
Materialized view log created.
Sub query materialized views
The following statement creates a sub query materialized view based on the emp and dept tables located on the remote database:
Sql> create materialized view  mv_empdept
As select * from emp e
Where exists
     (select * from dept d
     where e.deptno = d.deptno)
Refresh clause
[refresh [fast|complete|force]
               [on demand | commit]
               [start with date] [next date]
               [with {primary key|rowid}]]
The refresh option specifies:
  1. The refresh method used by oracle to refresh data in materialized view
  2. Whether the view is primary key based or row-id based
  3. The time and interval at which the view is to be refreshed

Refresh method - fast clause
The fast refreshes use the materialized view logs (as seen above) to send the rows that have changed from master tables to the materialized view.
You should create a materialized view log for the master tables if you specify the refresh fast clause.
Sql> create materialized view log on emp;
 
Materialized view log created.
Materialized views are not eligible for fast refresh if the defined subquery contains an analytic function.
Refresh method - complete clause
The complete refresh re-creates the entire materialized view. If you request a complete refresh, oracle performs a complete refresh even if a fast refresh is possible.
Refresh method - force clause
When you specify a force clause, oracle will perform a fast refresh if one is possible or a complete refresh otherwise. If you do not specify a refresh method (fast, complete, or force), force is the default.

Primary key and rowid clause
With primary key is used to create a primary key materialized view i.e. The materialized view is based on the primary key of the master table instead of rowid (for rowid clause). Primary key is the default option. To use the primary key clause you should have defined primary key on the master table or else you should use rowid based materialized views.
Primary key materialized views allow materialized view master tables to be reorganized without affecting the eligibility of the materialized view for fast refresh.
Rowid materialized views should have a single master table and cannot contain any of the following:
  • Distinct or aggregate functions
  • Group by subqueries , joins & set operations

Timing the refresh
The start with clause tells the database when to perform the first replication from the master table to the local base table. It should evaluate to a future point in time. The next clause specifies the interval between refreshes
Sql> create materialized view mv_emp_pk
         refresh fast
         start with sysdate
         next  sysdate + 2
         with primary key
         as select * from emp;

Materialized view created.
In the above example, the first copy of the materialized view is made at sysdate and the interval at which the refresh has to be performed is every two days.

Comments

Popular posts from this blog

Dependency Management - Dependencies in Server-Side PL/SQL

Dependencies Among Schema Objects

Dependency Injection in PL/SQL – Remove Hard Coded Dependencies from your code