What is Oracle views?
-A view is a representation of data from one or more tables or views.
-A view is a named and validated SQL query which is stored in the Oracle data dictionary.
–Views do not contain any data – it is just a stored query in the database that can be executed when called. All the data it shows comes from the base tables. One can think of a view as a virtual table or mapping of data from one or more
-A view takes up no storage space other than for the definition of the view in the data dictionary.
– A view can be used to represent a subset of data (for security issues), used to represent a superset of data (join several tables to one view), to hide complex joins, to provide meaningful names for columns and to minimize application and data source changes.
-The referenced tables are called base tables.
– In order to create view use the CREATE VIEW command followed by a SQL query.
|CREATE VIEW view_name AS
-You can specify the column names using CREATE VIEW (col1, col2…) AS SELECT COL1, COL2… FROM …;
-Creating a view using CREATE FORCE VIEW will create the view with compilation errors.
-You can create a read-only view using the WITH READ ONLY clause.
-Oracle 9i allows creating constraints on views.
-Constraints on views are not enforced, they are declarative constraints. To enforce constraints you must create the constraints on the base tables. You must always include the DISABLE NOVALIDATE clause when creating constraints on views.
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.
Example of Views
|Create view emp_data as select e.empno,e.ename,e.sal,e.deptno,d.dname
From emp e, dept d where e.deptno=d.deptno;
|Create view emp_high_sal as select empno,ename,sal
From emp where sal > 100000;
How to Modify the Views
We can modify the views using create or replace view command. This allows altering view without dropping, recreating and re-granting object privileges.
|Create or replace view emp_high_sal as select empno,ename,sal
From emp where sal > 200000;
This command create the view if it is not existent and modify the view if it is existent. All the plsql units which are accessing the view will become invalid after the view modification
How to recompile the views
Recompiling a view is done using the
ALTER VIEW <view_name> COMPILE;
|Alter view emp_high_sal compile;|
How to drop the view
Dropping a view is done using DROP VIEW command.
Dropping a view has no effect on the base tables on which view is based. Users who were accessing the data of the base tables using view will no longer be able to access the data of the base tables.
DROP VIEW view_name;
|Drop view emp_high_sal ;
How to perform DML operations on Views
-We can perform DML operations like insert,delete and update on the view also with some restrictions
-A VIEW in Oracle is created by joining one or more tables. When you update record(s) in a VIEW, it updates the records in the underlying tables that make up the View.
-But off course you need the appropriate privileges for the underlying tables
-DML operations on views can be performed as long as the view does not contain these words: DISTINCT, GROUP BY, START WITH, CONNECT BY, ROWNUM, UNION [ALL], INTERSECT, MINUS or view has subqueries.
-When using the WITH CHECK OPTION WITH CHECK OPTION [CONSTRAINT <constraint_name>] clause, one can restrict the values inserted using the views:
CREATE VIEW EMP_DATA AS SELECT EMPNO, ENAME FROM EMP WHERE EMPNO < 2000 WITH CHECK OPTION; One can use DML operation on this view only on values smaller than 2000.
-When specifying the constraint name, Oracle will create the constraint using specified name (otherwise will create using SYS_C followed by a unique string).
-In order to view allowable DML operations on a view, query the USER_UPDATABLE_COLUMNS view.
Creating Force Views
A view can be created even if the defining query of the view cannot be executed, using the CREATE FORCE VIEW command. Such a view is called a view with errors. This option can be useful for import/installation tools to create a view before the underlying objects are present.
What happens to view if the base tables are dropped?
If the underlying tables are dropped, then oracle does not dropped the view automatically. It remains in the database and it will be in invalid state. If the underlying tables are recreated, it will become valid again
Dictionary Views for seeing the View data
View details can be queried from the dictionary by querying either USER_VIEWS, ALL_VIEWS or DBA_VIEWS. Views are useful for security and information hiding, but can cause problems if nested too deeply. Some of the advantages of using views:
- Reduce the complexity of SQL statements
- Share only specific rows in a table with other users
- Hide the NAME and OWNER of the base table
- There are three categories of views
|USER_%||This view contain information of the objects owned by the user only
|ALL-%||This view contains information of the objects which the user can access in the database.
|DBA_%||This view contain information of the all objects in the system and these are restricted views which are accessible to the user who have DBA role
|DBA_% views about view information||ALL_% views about view information||USER_% views about view information|
|Column which can be updated||DBA_UPDATABLE_COLUMNS||ALL_UPDATABLE_COLUMNS||USER_UPDATABLE_COLUMNS|
|View about view information||dba_views||all_views||user_views|
To list all views owned by the current use
select view_name from user_views;
To list all views in a database:
Select owner,view_name from dba_views;
To list views accessible to the current user:
select view_name from all_views
To describe the view in sqlplus
How to determine the query of the already created view
Query the TEXT column of table DBA_VIEWS.
SQL> set long 10000
SQL> select TEXT
2 FROM DBA_VIEWS
3 where OWNER = ‘<owner_name>’
4 and VIEW_NAME = ‘<view_name>’;
How to extract the view definition (DDL statements) from an Oracle database without having to go through a stack of dictionary views
SQL> set long 1000
SQL> set pagesize 0
select DBMS_METADATA.GET_DDL(‘VIEW’,'<view_name>’) from DUAL