Oracle8i SQL Reference
Release 3 (8.1.7)

Part Number A85397-01





Go to previous page Go to beginning of chapter Go to next page

SQL Statements:



Use the CREATE VIEW statement to define a view, which is a logical table based on one or more tables or views. A view contains no data itself. The tables upon which a view is based are called base tables.

You can also create an object view or a relational view that supports LOB and object datatypes (object types, REFs, nested table, or varray types) on top of the existing view mechanism. An object view is a view of a user-defined type, where each row contains objects, each object with a unique object identifier.

See Also:



To create a view in your own schema, you must have CREATE VIEW system privilege. To create a view in another user's schema, you must have CREATE ANY VIEW system privilege.

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.

To use the basic constructor method of an object type when creating an object view, one of the following must be true:

Partition Views

Partition views were introduced in Release 7.3 to provide partitioning capabilities for applications requiring them. Partition views are supported in Oracle8i so that you can upgrade applications from Release 7.3 without any modification. In most cases, subsequent to migration to Oracle8i you will want to migrate partition views into partitions.

With Oracle8i, you can use the CREATE TABLE statement to create partitioned tables easily. Partitioned tables offer the same advantages as partition views, while also addressing their shortcomings. Oracle recommends that you use partitioned tables rather than partition views in most operational environments.

See Also:



subquery: See SELECT and subquery.


Keywords and Parameters


Specify OR REPLACE to re-create the view if it already exists. You can use this clause to change the definition of an existing view without dropping, re-creating, and regranting object privileges previously granted on it.

INSTEAD OF triggers defined in the view are dropped when a view is re-created.

If any materialized views are dependent on view, those materialized views will be marked UNUSABLE and will require a full refresh to restore them to a usable state. Invalid materialized views cannot be used by query rewrite and cannot be refreshed until they are recompiled.

See Also:



Specify FORCE if you want to create the view regardless of whether the view's base tables or the referenced object types exist or the owner of the schema containing the view has privileges on them. These conditions must be true before any SELECT, INSERT, UPDATE, or DELETE statements can be issued against the view.


Specify NOFORCE if you want to create the view only if the base tables exist and the owner of the schema containing the view has privileges on them. This is the default.


Specify the schema to contain the view. If you omit schema, Oracle creates the view in your own schema.


Specify the name of the view or the object view.

Restriction: If a view has INSTEAD OF triggers, any views created on it must have INSTEAD OF triggers, even if the views are inherently updatable.


Specify names for the expressions selected by the view's query. The number of aliases must match the number of expressions selected by the view. Aliases must follow the rules for naming Oracle schema objects. Aliases must be unique within the view.

If you omit the aliases, Oracle derives them from the columns or column aliases in the view's query. For this reason, you must use aliases if the view's query contains expressions rather than only column names.

Restriction: You cannot specify an alias when creating an object view.

See Also: "Syntax for Schema Objects and Parts in SQL Statements" 

OF type_name

Use this clause to explicitly creates an object view of type type_name. The columns of an object view correspond to the top-level attributes of type type_name. Each row will contain an object instance and each instance will be associated with an object identifier (OID) as specified in the WITH OBJECT IDENTIFIER clause. If you omit schema, Oracle creates the object view in your own schema.


The WITH OBJECT IDENTIFIER lets you specify the attributes of the object type that will be used as a key to identify each row in the object view. In most cases these attributes correspond to the primary-key columns of the base table. You must ensure that the attribute list is unique and identifies exactly one row in the view.

If you try to dereference or pin a primary key REF that resolves to more than one instance in the object view, Oracle raises an error. 


Note: The 8.0 syntax WITH OBJECT OID is replaced with this syntax for clarity. The keywords WITH OBJECT OID are supported for backward compatibility, but Oracle Corporation recommends that you use the new syntax WITH OBJECT IDENTIFIER.



If the object view is defined on an object table or an object view, you can omit this clause or specify DEFAULT



Specify DEFAULT if you want Oracle to use the intrinsic object identifier of the underlying object table or object view to uniquely identify each row. 



Specify an attribute of the object type from which Oracle should create the object identifier for the object view. 

See Also: CREATE TYPE for more information about creating objects 

AS subquery

Specify a subquery that identifies columns and rows of the table(s) that the view is based on. The subquery's select list can contain up to 1000 expressions.

If you create views that refer to remote tables and views, the database links you specify must have been created using the CONNECT TO clause of the CREATE DATABASE LINK statement, and you must qualify them with schema name in the view query.

Restrictions on the view query:

The preceding restrictions apply to materialized views as well.


Use the with_clause to restrict the subquery in one of the following ways:


Specify WITH READ ONLY if you want no deletes, inserts, or updates to be performed through the view. 


Specify WITH CHECK OPTION to guarantee that inserts and updates performed through the view will result in rows that the view query can select. The CHECK OPTION cannot make this guarantee if:

  • There is a subquery in the query of this view or any view on which this view is based or

  • INSERT, UPDATE, or DELETE operations are performed using INSTEAD OF triggers.


CONSTRAINT constraint 

Specify the name of the CHECK OPTION constraint. If you omit this identifier, Oracle automatically assigns the constraint a name of the form SYS_Cn, where n is an integer that makes the constraint name unique within the database. 


Basic View Example

The following statement creates a view of the emptable named dept20. The view shows the employees in Department 20 and their annual salary:

    AS SELECT ename, sal*12 annual_salary 
        FROM emp 
        WHERE deptno = 20; 

The view declaration need not define a name for the column based on the expression sal*12, because the subquery uses a column alias (annual_salary) for this expression.

Updatable View Example

The following statement creates an updatable view named clerk of all clerks in the emp table. Only the employees' IDs, names, and department numbers are visible in this view and only these columns can be updated in rows identified as clerks:

CREATE VIEW clerk (id_number, person, department, position) 
    AS SELECT empno, ename, deptno, job 
       FROM emp 
       WHERE job = 'CLERK'

Because of the CHECK OPTION, you cannot subsequently insert a new row into clerk if the new employee is not a clerk.

Join View Example

A join view is one whose view query contains a join. If at least one column in the join has a unique index, then it may be possible to modify one base table in a join view. You can query USER_UPDATABLE_COLUMNS to see whether the columns in a join view are updatable. For example:

   SELECT e.empno, e.ename, d.deptno, d.loc
      FROM emp e, dept d
   WHERE e.deptno = d.deptno

View created.

SELECT column_name, updatable 
  FROM user_updatable_columns
  WHERE table_name = 'ED';

--------------- ---
ENAME           YES
DEPTNO          NO
EMPNO           YES
LOC             NO

INSERT INTO ed (ENAME, EMPNO) values ('BROWN', 1234);

In the above example, there is a unique index on the deptno column of the dept table. You can insert, update or delete a row from the emp base table, because all the columns in the view mapping to the emp table are marked as updatable and because the primary key of emp is included in the view.

Note: You cannot insert into the table using the view unless the view contains all NOT NULL columns of all tables in the join, unless you have specified DEFAULT values for the NOT NULL columns. 

See Also: Oracle8i Application Developer's Guide - Fundamentals for more information on updating join views. 

Read-Only View Example

The following statement creates a read-only view named clerk of all clerks in the emp table. Only the employees' IDs, names, department numbers, and jobs are visible in this view:

CREATE VIEW clerk (id_number, person, department, position)
     AS SELECT empno, ename, deptno, job 
       FROM emp 
       WHERE job = 'CLERK'

Object View Example

The following example creates object view emp_object_view of employee_type:

  ( empno       NUMBER(4),
    ename       VARCHAR2(20), 
    job         VARCHAR2(9), 
    mgr         NUMBER(4),
    hiredate    DATE, 
    sal         NUMBER(7,2), 
    comm        NUMBER(7,2)  );

CREATE OR REPLACE VIEW emp_object_view OF employee_type 
  AS SELECT empno, ename, job, mgr, hiredate, sal, comm 
     FROM emp; 

Go to previous page Go to beginning of chapter Go to next page
Copyright © 1996-2000, Oracle Corporation.

All Rights Reserved.