SQL Tutorial - View: Chapter 21: Views
SQL Tutorial - View: Chapter 21: Views
SQL Tutorial - View: Chapter 21: Views
For example, if we wanted a sub ordinate to access EMP table without the details of salaries then
we can
create a view for the sub ordinate with only required columns.
VIEW created
SQL> SELECT * FROM na_emp_v;
Create a view with specified columns:-
SQL> CREATE OR REPLACE VIEW <span STYLE="background-color: yellow;"> na_emp_v </span> (empno, e
deptno FROM emp;
--View created.
SQL> SELECT * FROM na_emp_v;
Dropping a view:-
Syntax:-
SQL> DROP VIEW view_name;
Example:-
SQL> DROP VIEW na_emp_v;
VIEW dropped.
Examples:-
We can use DEPT and EMP tables for creating complex views.
SQL> SELECT * FROM dept;
SQL> SELECT * FROM emp;
SQL> CREATE OR REPALCE VIEW na_emp_v AS
SELECT e.empno,
e.ename,
e.sal,
e.deptno,
d.dname,
d.loc FROM emp e,dept d
WHERE e.deptno = d.deptno;
VIEW created.
Once the view is created then we can query the data from view.
SQL> SELECT * FROM na_emp_v;
Creating a complex view by using group functions:-
VIEW created.
Query data from view
Dropping a view:-
Syntax:-
VIEW dropped.
For delete: -
It is not possible to delete when the view is derived from more than one database table.
For update: -
It is not possible to update when the view is derived from more than one database table.
It is not possible to update the columns which are derived from arithmetic expression
andfunction from pseudo columns.
For insertion:-
o It is not possible to insert when the view is derived from more than one database table.
o It is not possible to insert into the columns which are derived from arithmetic expression
andfunction from pseudo columns.
o It is not possible to insert the data into the view if the mandatory/primary key columns of
thebase tables are missing in the view definition.
DML operations on simple views:-
For better understanding lets create a sample table first.
SQL> CREATE TABLE na_dept (deptno NUMBER (10), dname VARCHAR2(10), loc VARCHAR2(10)); --tab
Once the table is created, let’s create a simple view on top of it.
SQL> CREATE VIEW na_dept_view AS SELECT *FROM na_dept;
-- View created.
/*NA_DEPT_VIEW don’t have any records as this is created on top of a empty table*/
Insert record into NA_DEPT_VIEW to know whether DML operations
will reflect the base table or not.
SQL>INSERT INTO na_dept_view VALUES (10,'ACCOUNTING','ENGLAND');
/*Data in View*/
SQL>SELECT * FROM na_dept_view;
*Similarly we can perform other DML operations like update and delete on the view which
internally affects the base table.
-- View created.
When we try to insert record into the complex view system throws an error, as there are multiple
tables involved.
INSTEAD OF TRIGGERS:-
o Instead of triggers can be used only with views.
o Effective for views in which there are multiple tables involved.
Let’s create INSERTED OF TRIGGER on ‘na_complex_v’. Inside trigger, we will write a plsql code
to insert data into EMP and DEPT separately as our view is based on these two tables.
SQL>CREATE OR REPLACE TRIGGER na_instd_of_trig INSTEAD OF INSERT ON na_complex_v FOR EACH R
INSERT INTO DEPT (deptno,dname,loc) VALUES (:NEW.deptno, :NEW.dname, :NEW.LOC); END;
/* :new key word is used to refer to the values in INSERT statements. */
Once the trigger is created lets insert the data into complex view i.e. ‘na_complex_v’
Output:
A materialized view is a database object that contains the results of a query.
Materialized View is a static view that holds data in it.
Materialized view takes the snapshot of the data whenever it is created or refreshed.
Materialized view does not support DML operations on it.
To create a “materialized view “user should have permission in schema.
It is used to maintain historic data.
It is used for data analysis and reporting purpose.
Materialized are mainly created to over come the performance issues and to store historic data.
The determines when Materialized View is built:
Refresh Options
Refresh Modes-ON COMMIT –refreshes occur whenever a commit is performed on one of the
view’s underlying detail table(s).
o Available only with single table aggregate or join based views
o Keeps view data transactionally accurate
o Need to check alert log for view creation errors
ON DEMAND –Refreshes are initiated manually using one of the procedures in the
DBMS_MVIEW package.
Can be used with all types of materialized views
Manual Refresh Procedures
DBMS_MVIEW.REFRESH(,)
DBMS_MVIEW.REFRESH_ALL_MVIEWS()
Example 1:
Example 2:
SQL> CREATE MATERIALIZED VIEW items_summary_mv ON PREBUILT TABLE
REFRESH FORCE AS
SELECT a.PRD_ID,
a.SITE_ID,
a.TYPE_CODE,
a.CATEG_ID,
SUM(a.GMS) GMS,
SUM(a.NET_REV) NET_REV,
SUM(a.BOLD_FEE) BOLD_FEE,
SUM(a.BIN_PRICE) BIN_PRICE,
SUM(a.GLRY_FEE) GLRY_FEE,
SUM(a.QTY_SOLD) QTY_SOLD,
COUNT(a.ITEM_ID) UNITS
FROM na_items a GROUP BY a.PRD_ID, a.SITE_ID, a.TYPE_CODE, a.CATEG_ID;
Normal View:-
It is a stored select statement
It is a virtual component
It allows DESC, DML, SELECT on it
It is stored permanently in “user_views” system table.
It can be shared with other users
DML on view are reflected in table and DML on table are reflected in view
It is used to share “selected rows and columns” with other rows
It is used for reporting purpose
It will improve the performance while manipulating or retrieving data through views
Materialized view:-
It is a static view
It holds data in it
It will not support DML on it
DML on table will not be reflected in view
To create it “create materialized view “permission is required.
It is used to maintain historic data.
It is used for data analysis and reporting purpose.
It is same as SNAP SHOT (defined by DBA only)
A view can be created even if the defining query of the view cannot be executed. We call such a
view
as view with errors.
For example, if a view refers to a non-existent table or an invalid column of an existing table or if
the
owner of the view does not have the required privileges, then the view can still be created and
entered
into the data dictionary.
We can create such views (i.e. view with errors) by using the FORCE option in
the CREATE VIEW command:
Example: – In this example we are trying to create a view using a table that does not exist in
the database.
/* dummy_table is not available in the database*/
CREATE OR REPLACE FORCE VIEW na_view
AS SELECT * FROM dummy_table;
Output:-
Warning: View created with compilation errors.
Specify FORCE if we want to create the view regardless of whether the base tables of the view or
the
referenced object types exist or the owner of the schema containing the view has privileges on
them.
The advantage of force view is that in future if view script becomes valid, we can start using the
view.