CREATE VIEW

The CREATE VIEW statement creates a view on tables or views at the current server.

Invocation

This statement can be embedded in an application program or issued interactively. It is an executable statement that can be dynamically prepared only if DYNAMICRULES run behavior is implicitly or explicitly specified.

Authorization

For every table or view identified in the fullselect, the privilege set that is defined below must include at least one of the following:

  • The SELECT privilege on the table or view
  • Ownership of the table or view
  • DBADM authority for the database (tables only)
  • Start of changeDATAACCESS authorityEnd of change
  • SYSADM authority
  • Start of changeSQLADM authority (catalog tables only)End of change
  • Start of changeSystem DBADM authority (catalog tables only)End of change
  • Start of changeACCESSCTRL authority (catalog tables only)End of change
  • SYSCTRL authority (catalog tables only)
  • Start of changeSECADM authority (catalog tables only)End of change

If the database is implicitly created, the database privileges must be on the implicit database or on DSNDB04.

Authority requirements depend in part on the choice of the owner of the view. For information on how to choose the owner, see the description of view-name in ALTER VIEW.

Privilege set: If the statement is embedded in an application program, the privilege set is the privileges that are held by the of the owner of the plan or package. If the application is bound in a trusted context with the ROLE AS OBJECT OWNER clause specified, a role is the owner. Otherwise, an authorization ID is the owner.

  • If this privilege set includes SYSADM authority, the owner of the view can be any authorization ID. If that set includes SYSCTRL but not SYSADM authority, the following is true: the owner of the view can be any authorization ID, provided the view does not refer to user tables or views in the first FROM clause of its defining fullselect. (It could refer instead, for example, to catalog tables or views thereof.)

    If the view satisfies the rules in the preceding paragraph, and if no errors are present in the CREATE statement, the view is created, even if the owner has no privileges at all on the tables and views identified in the fullselect of the view definition.

  • Start of changeIf the privilege set includes system DBADM authority, the owner of the view can be any authorization ID. However, to create a view on a user table, either the owner of the view or the creator must have the SELECT privilege on all the tables or views in the CREATE VIEW statement.End of change
  • If the privilege set lacks Start of changesystem DBADM, End of changeSYSADM and SYSCTRL but includes DBADM authority on at least one of the databases that contains a table from which the view is created, the owner of the view can be any authorization ID if all of the following conditions are true:
    • The value of field DBADM CREATE AUTH was set to YES on panel DSNTIPP during DB2 installation.
    • The view is not based only on views.
    Note: The owner of the view must have the SELECT privilege on all tables and views in the CREATE VIEW statement, or, if the owner does not have the SELECT privilege on a table, the creator must have DBADM authority on the database that contains that table.
  • If the privilege set lacks SYSADM, SYSCTRL, Start of changesystem DBADM, End of changeand DBADM authority, or if the authorization ID of the application plan or package fails to meet any of the previous conditions, the owner of the view must be the owner of the application plan or package.

If ROLE AS OBJECT OWNER is in effect, the schema qualifier must be the same as the role, unless the role has the CREATEIN privilege on the schema, SYSADM authority, Start of changesystem DBADM authority, End of changeor SYSCTRL authority.

If ROLE AS OBJECT OWNER is not in effect, one of the following rules applies:

  • If the privilege set lacks the CREATIN privilege on the schema, SYSADM authority, Start of changesystem DBADM authority, End of changeor SYSCTRL authority, the schema qualifier (implicit or explicit) must be the same as one of the authorization ids of the process.
  • If the privilege set includes Start of changesystem DBADM authority, End of changeSYSADM authority or SYSCTRL authority, the schema qualifier can be any valid schema name.

If the statement is dynamically prepared, the following rules apply:

  • If the SQL authorization ID of the process has SYSADM authority, the owner of the view can be any authorization ID. If that authorization ID has SYSCTRL but not SYSADM authority, the following is true: the owner of the view can be any authorization ID, provided the view does not refer to user tables or views in the first FROM clause of its defining fullselect. (It could refer instead, for example, to catalog tables or views thereof.)

    If the view satisfies the rules in the preceding paragraph, and if no errors are present in the CREATE statement, the view is created, even if the owner has no privileges at all on the tables and views identified in the fullselect of the view definition.

  • Start of changeIf the SQL authorization ID of the process has system DBADM authority, the owner of the view can be any authorization ID. However, to create a view on a user table, either the owner of the view or the SQL authorization ID must have the SELECT privilege on all the tables or views in the CREATE VIEW statement.End of change
  • If SQL authorization ID of the process lacks Start of changesystem DBADM authority, End of changeSYSADM and SYSCTRL but includes DBADM authority on at least one of the databases that contains a table from which the view is created, the owner of the view can be different from the SQL authorization ID if all of the following conditions are true:
    • The value of field DBADM CREATE AUTH was set to YES on panel DSNTIPP during DB2 installation.
    • The view is not based only on views.
    Note: The owner of the view must have the SELECT privilege on all tables and views in the CREATE VIEW statement, or, if the owner does not have the SELECT privilege on a table, the creator must have DBADM authority on the database that contains that table.
  • If the SQL authorization ID of the process lacks SYSADM, SYSCTRL, Start of changesystem DBADM authority, End of changeor DBADM authority, or if the SQL authorization ID of the process fails to meet any of the previous conditions, only the authorization IDs of the process can own the view. In this case, the privilege set is the privileges that are held by the authorization ID selected for ownership.

Syntax

Read syntax diagram
>>-CREATE VIEW--view-name--+---------------------+--AS---------->
                           |   .-,-----------.   |       
                           |   V             |   |       
                           '-(---column-name-+-)-'       

>--+-----------------------------------+--fullselect------------>
   |       .-,-----------------------. |               
   |       V                         | |               
   '-WITH----common-table-expression-+-'               

>--+----------------------------------+------------------------><
   |       .-CASCADED-.               |   
   '-WITH--+----------+--CHECK OPTION-'   
           '-LOCAL----'                   

Description

view-name
Start of changeNames the view. Start of changeThe name, including the implicit or explicit qualifier, must not identify a table, view, alias, or synonym that exists at the current server or a table that exists in the SYSIBM.SYSPENDINGOBJECTS catalog table.End of change The unqualified name must not be the same as an existing synonym.

If the name is qualified, the name can be a two-part or three-part name. If a three-part name is used, the first part must match the value of the field DB2 LOCATION NAME of installation panel DSNTIPR at the current server. (If the current server is not the local DB2, this name is not necessarily the name in the CURRENT SERVER special register.)

End of change
column-name,…
Names the columns in the view. If you specify a list of column names, it must consist of as many names as there are columns in the result table of the fullselect. Each name must be unique and unqualified. If you do not specify a list of column names, the columns of the view inherit the names of the columns of the result table of the fullselect.

You must specify a list of column names if the result table of the fullselect has duplicate column names or an unnamed column (a column derived from a constant, function, or expression that was not given a name by the AS clause). For more details about unnamed columns, see the information about names of result columns under select-clause.

AS
Identifies the view definition.
WITH common-table-expression
Defines a common table expression for use with the fullselect that follows. Start of changeThe fullselect must not contain a period specification.End of change For an explanation of common table expression, see common-table-expression.
fullselect
Defines the view. At any time, the view consists of the rows that would result if the fullselect were executed.

Start of changeThe fullselect must conform to the following rules:End of change

Start of change
  • The fullselect must not refer to any host variables or parameter markers (question marks).
  • The fullselect must not refer to any declared temporary tables.
  • Start of changeThe fullselect must not reference an accelerator-only table.End of change
  • Start of changeThe fullselect must not include an invocation of the UNPACK function.End of change
  • Start of changeThe fullselect must not contain a period specification.End of change
  • The FROM clause of the fullselect must not include a data-change-table-reference.
  • The FROM clause of the fullselect must not include a view for which an INSTEAD OF trigger is defined.
End of change

For an explanation of fullselect, see fullselect.

WITH CASCADED CHECK OPTION or WITH LOCAL CHECK OPTIONS
Specifies that every row that is inserted or updated through the view must conform to the definition of the view. A row that does not conform to the definition of the view is a row that cannot be retrieved using that view.

The CHECK OPTION clause must not be specified if the view is read-only, includes a subquery, references a function that is not deterministic or has an external action, or if the fullselect of the view refers to a created temporary table. If the CHECK OPTION clause is specified for an updatable view that does not allow inserts, it applies to updates only.

If the CHECK OPTION clause is omitted, the definition of the view is not used in the checking of any insert or update operations that use the view. Some checking might still occur during insert or update operations if the view is directly or indirectly dependent on another view that includes the CHECK OPTION clause. Because the definition of the view is not used, rows might be inserted or updated through the view that do not conform to the definition of the view.

The difference between the two forms of the check option, CASCADED and LOCAL, is meaningful only when a view is dependent on another view. The default is CASCADED. The view on which another view is directly or indirectly defined is an underlying view.
CASCADED
Update and insert operations on view V must satisfy the search conditions of view V and all underlying views, regardless of whether the underlying views were defined with a check option. Furthermore, every updatable view that is directly or indirectly defined on view V inherits those search conditions (the search conditions of view V and all underlying views of V) as a constraint on insert or update operations. WITH CASCADED CHECK OPTION must not be specified if a view on which the specified view definition is dependent has an INSTEAD OF trigger defined.
LOCAL
Update and insert operations on view V must satisfy the search conditions of view V and underlying views that are defined with a check option (either WITH CASCADED CHECK OPTION or WITH LOCAL CHECK OPTION). Furthermore, every updatable view that is directly or indirectly defined on view V inherits those search conditions (the search conditions of view V and all underlying views of V that are defined with a check option) as a constraint on insert or update operations.

The LOCAL form of the CHECK option lets you update or insert rows that do not conform to the search condition of view V. You can perform these operations if the view is directly or indirectly defined on a view that was defined without a check option.

Table 1 illustrates the effect of using the default check option, CASCADED. The information in Table 1 is based on the following views:

  • CREATE VIEW V1 AS SELECT COL1 FROM T1 WHERE COL1 > 10
  • CREATE VIEW V2 AS SELECT COL1 FROM V1 WITH CASCADED CHECK OPTION
  • CREATE VIEW V3 AS SELECT COL1 FROM V2 WHERE COL1 < 100
Table 1. Examples using default check option, CASCADED
SQL statement Description of result
INSERT INTO V1 VALUES(5) Succeeds because V1 does not have a check option and it is not dependent on any other view that has a check option.
INSERT INTO V2 VALUES(5) Results in an error because the inserted row does not conform to the search condition of V1 which is implicitly is part of the definition of V2.
INSERT INTO V3 VALUES(5) Results in an error because the inserted row does not conform to the search condition of V1.
INSERT INTO V3 VALUES(200) Succeeds even though it does not conform to the definition of V3 (V3 does not have the view check option specified); it does conform to the definition of V2 (which does have the view check option specified).

The difference between CASCADED and LOCAL is shown best by example. Consider the following updatable views, where x and y represent either LOCAL or CASCADED:

  • V1 is defined on Table T0.
  • V2 is defined on V1 WITH x CHECK OPTION.
  • V3 is defined on V2.
  • V4 is defined on V3 WITH y CHECK OPTION.
  • V5 is defined on V4.

This example shows V1 as an underlying view for V2 and V2 as dependent on V1.

Table 2 shows the views in which search conditions are checked during an insert or update operation:
Table 2. Views in which search conditions are checked during insert and update operations
View used in
INSERT or
UPDATE operation
x = LOCAL
y = LOCAL
x = CASCADED
y = CASCADED
x = LOCAL
y = CASCADED
x = CASCADED
y = LOCAL
V1 None None None None
V2 V2 V2, V1 V2 V2, V1
V3 V2 V2, V1 V2 V2, V1
V4 V4, V2 V4, V3, V2, V1 V4, V3, V2, V1 V4, V2, V1
V5 V4, V2 V4, V3, V2, V1 V4, V3, V2, V1 V4, V2, V1

Notes

Owner privileges
The owner of a view always acquires the SELECT privilege on the view and the authority to drop the view. If all of the privileges that are required to create the view are held with the GRANT option before the view is created, the owner of the view receives the SELECT privilege with the GRANT option. Otherwise, the owner receives the SELECT privilege without the GRANT option. For example, assume that a view definition also refers to a user-defined function. If the owner's EXECUTE privilege on the user-defined function is held without the GRANT option, the owner acquires the SELECT privilege on the view without the GRANT option.

The owner can also acquire INSERT, UPDATE, and DELETE privileges on the view. Acquiring these privileges is possible if the view is not "read-only", which means a single table of view is identified in the first FROM clause of the fullselect. For each privilege that the owner has on the identified table or view (INSERT, UPDATE, and DELETE) before the new view is created, the owner acquires that privilege on the view. The owner receives the privilege with the GRANT option if the privilege is held on the table or view with the GRANT option. Otherwise, the owner receives the privileges without the GRANT option.

With appropriate DB2 authority, a process can create views for those who have no authority to create the views themselves. The owner of such a view has the SELECT privilege on the view, without the GRANT option, and can drop the view.

For more information on the ownership of an object, see Start of changeAuthorization, privileges, permissions, masks, and object ownershipEnd of change.

Authorization for views created for other users:
When a process with appropriate authority creates a view for another user that does not have authorization for the underlying table or view, the SELECT privilege for the created view is implicitly granted to the user.
Start of changeConsiderations for row access control and column access control:End of change
Start of changeThe view definition might reference a table for which row access control or column access control is activated. If the view definition references a table for which row access control or column access control is activated, the WITH CHECK OPTION clause must not be specified if the search conditions from the view or from the underlying views will be checked during an insert or update operation. Note that the WITH CHECK OPTION clause is ignored if such search conditions do not exist.End of change
Read-only views:
A view is read-only if one or more of the following statements is true of its definition:
  • The first FROM clause identifies more than one table or view, or identifies a table function, a nested table expression, or a common table expression.
  • The first SELECT clause specifies the keyword DISTINCT.
  • The outer fullselect contains a GROUP BY clause.
  • The outer fullselect contains a HAVING clause.
  • The first SELECT clause contains an aggregate function.
  • It contains a subquery such that the base object of the outer fullselect, and of the subquery, is the same table.
  • The first FROM clause identifies a read-only view.
  • The first FROM clause identifies a system-maintained materialized query table.
  • The outer fullselect is not a subselect (contains a set operator).

A read-only view cannot be the object of an SQL data change statement or a TRUNCATE statement. A view that includes GROUP BY or HAVING cannot be referred to in a subquery of a basic predicate.

Insertable views:
A view is insertable if an INSTEAD OF trigger for the insert operation has been defined for the view, or if at least one column of the view is updatable (independent of an INSTEAD OF trigger for update).
Considerations for implicitly hidden columns:
It is possible that the result table of the fullselect will include a column of a base table that is defined as implicitly hidden. This can occur when the implicitly hidden column is explicitly referenced in the fullselect of the view definition. However, the corresponding column of the view does not inherit the implicitly hidden attribute. Columns of a view cannot be defined as hidden.
Testing a view definition:
You can test the semantics of your view definition by executing SELECT * FROM view-name.
The two forms of a view definition:
Both the source and the operational form of a view definition are stored in the DB2 catalog. Those two forms are not necessarily equivalent because the operational form reflects the state that exists when the view is created. For example, consider the following statement:
   CREATE VIEW V AS SELECT * FROM S;
In this example, S is a synonym or alias for A.T, which is a table with columns C1, C2, and C3®. The operational form of the view definition is equivalent to:
   SELECT C1, C2, C3 FROM A.T;

Adding columns to A.T using ALTER TABLE and dropping S does not affect the operational form of the view definition. Thus, if columns are added to A.T or if S is redefined, the source form of the view definition can be misleading.

View restrictions:
A view definition cannot contain references to remote objects. A view definition cannot map to more than 15 base table instances. A view definition cannot reference a declared global temporary table.
Start of changeConsiderations for inline LOB columns:End of change
Start of changeIf the view references a table that contains an inline LOB column and DB2 determines that the inline attribute can be passed on to the view, the view will then inherit the inline attribute, otherwise the inline attribute is not inherited by the view.End of change
Start of changeConsiderations for XML columns:End of change
Start of changeIf the view has an XML column and the column of the underlying base table for the view has an XML type modifier, the view column has the same type modifier. However, if there is an instead of trigger defined on the view, validation of the column, according to XML schemas in the type modifier, is not enforced during insert or update to this view.End of change

Examples

Example 1: Create the view DSN8A10.VPROJRE1. PROJNO, PROJNAME, PROJDEP, RESPEMP, FIRSTNME, MIDINIT, and LASTNAME are column names. The view is a join of tables and is therefore read-only.
   CREATE VIEW DSN8A10.VPROJRE1
     (PROJNO,PROJNAME,PROJDEP,RESPEMP,
      FIRSTNME,MIDINIT,LASTNAME)
     AS SELECT ALL
     PROJNO,PROJNAME,DEPTNO,EMPNO,
     FIRSTNME,MIDINIT,LASTNAME
     FROM DSN8A10.PROJ, DSN8A10.EMP
     WHERE RESPEMP = EMPNO;

In the example, the WHERE clause refers to the column EMPNO, which is contained in one of the base tables but is not part of the view. In general, a column named in the WHERE, GROUP BY, or HAVING clause need not be part of the view.

Example 2: Create the view DSN8A10.FIRSTQTR that is the UNION ALL of three fullselects, one for each month of the first quarter of 2000. The common names are SNO, CHARGES, and DATE.
   CREATE VIEW DSN8A10.FIRSTQTR (SNO, CHARGES, DATE) AS
  SELECT SNO, CHARGES, DATE
  FROM MONTH1
  WHERE DATE BETWEEN '01/01/2000' and '01/31/2000' 
    UNION All
  SELECT SNO, CHARGES, DATE
  FROM MONTH2
  WHERE DATE BETWEEN '02/01/2000' and '02/29/2000' 
    UNION All
  SELECT SNO, CHARGES, DATE
  FROM MONTH3
  WHERE DATE BETWEEN '03/01/2000' and '03/31/2000';