Entering content frame

CREATE VIEW Statement (create_view_statement) Locate the document in its SAP Library structure

The CREATE VIEW statement (create_view_statement) defines a view table.

View Table

A view table is a view of an existing table: parts of the table are hidden and other parts remain visible. You can use VIEW tables to truncate longer SELECT statements. You can use view tables to hide unimportant or confidential data.

A view table never actually exists physically. Instead, it is formed from the rows of the underlying base table(s) when this view table is specified in an SQL statement.

Syntax

<create_view_statement> ::=
  CREATE [OR REPLACE] VIEW <table_name> [(<alias_name>,...)] AS <query_expression> [WITH CHECK OPTION]

Explanation

If a schema is not specified in the view table name table_name, the current schema is assumed implicitly. The view table name must not be identical to the name of an existing table in the schema.

When the CREATE VIEW statement is executed, metadata that describes the view table is stored in the database catalog.

The view table is always identical to the table that would be obtained as the result of the QUERY expression query_expression. The QUERY expression must not contain a parameter specification. The QUERY expression must not reference a temporary table or a result table name.

The table expressions of the QUERY specification in the QUERY expression of the CREATE VIEW statement must not contain a QUERY expression.

If a column selected by the QUERY statement is of the data type LONG, the FROM clause must contain exactly one table name that is based on exactly one base table.

Join View Table

A join view table is a view table whose FROM clause contains more than one table or one join table.

Complex View Table

A view table is a complex view table if it satisfies one of the following conditions:

·        The definition of the view table contains DISTINCT or GROUP BY or HAVING.

·        The CREATE VIEW statement contains EXCEPT, INTERSECT, or UNION.

·        The search condition in the QUERY expression  of the CREATE VIEW statement contains a subquery.

·        The CREATE VIEW statement contains an outer join, that is an OUTER JOIN indicator outer_join_indicator in a JOIN predicate of the search condition.

Updateable View Table

A view table is deemed updateable if it is not a complex view table and if it is not based on a complex view table.

Updateable Join View Table

An updateable join view table is an updateable view table for which the following conditions must also be fulfilled:

·        Each base table on which the view table is based has a key defined by the user.

·        Referential CONSTRAINT definitions must exist between the base tables on which the view table is based.

·        One of the base tables, on which the view table is based, is not a referenced table referenced_table of a referential CONSTRAINT definition for a different base table of the view table. This table is the key table of the view table.

·        For each base table on which the view table is based, there is a sequence of referential CONSTRAINT definitions so that the respective base table can be accessed from the key table.

·        The referential CONSTRAINT definitions must be reflected as a JOIN predicate in the search condition of the CREATE VIEW statement, that is, the condition "key column = foreign key column" must exist for every column in each referential CONSTRAINT definition.

·        The CREATE VIEW statement must contain either the primary key or foreign key column from each referential CONSTRAINT definition as the selected column, but not both.

·        The view table must be defined with WITH CHECK OPTION.

For more information, see Updateable Join View Table.

Privileges

The user must have the SELECT privilege for all columns occurring in the view definition. The user is the owner of the view table and has at least the SELECT privilege for it. The user may grant the SELECT privilege for any columns in the view table derived from columns for which the user is authorized to grant the SELECT privilege to others.

The user only has the INSERT, UPDATE and DELETE privileges if he or she has the relevant privileges for the tables that form the basis of the view table and the join view table and only if the view table and the join view table are updateable. The user may only grant these privileges to others if he or she is authorized to grant the corresponding privilege for all tables on which the view table and join view table are based.

See also:

INSERT/UPDATE/DELETE Privilege for Owners of the View Table

Privilege Specification (priv_spec)

OR REPLACE

If OR REPLACE is not specified, the table name table_name must not be identical to the name of an existing view table.

If OR REPLACE is specified, the table name may be identical to the name of an existing view table. In this case, the definition of the existing view table is replaced by the new definition. The database system then attempts to adapt privileges granted for the existing view table to the new view definition, with the result that the privileges for the view table usually remain unchanged. Privileges are only removed implicitly if conflicts occur that cannot be resolved by the database system. If there are major discrepancies between the two view definitions, the CREATE VIEW statement may fail in the following case: the CREATE VIEW statement of a view table based on the existing view table cannot be executed correctly for the new view definition.

Alias Names (alias_name)

The column names of the view table must be unique. Otherwise, alias names must be specified for the result table generated by the QUERY expression. The number of alias names must be equal to the number of columns in the result table generated by the QUERY expression. If no alias names are specified, the column names of the result table generated by the QUERY expression are applied to the view table. The column descriptions for the view table are taken from the corresponding columns in the query expression. The FROM clause of the QUERY expression can contain one or more tables.

WITH CHECK OPTION

If the create view statement contains a WITH CHECK OPTION, the owner of the view table must have the INSERT, UPDATE, or DELETE privilege for the view table.

Specifying WITH CHECK OPTION has the effect that the insert statement or update statement issued on the view table does not create any rows that could not be selected subsequently via the view table; that is, the search condition of the view table must be true for any resulting rows.

The CHECK OPTION is inherited, that is, if a view table V was defined WITH CHECK OPTION and V occurs in the from clause of an updateable view table V1, only those rows that can be selected using V can be inserted or altered using V1.

 

Leaving content frame