CREATE VIEW statement

Views are virtual tables formed by a query. A view is a dictionary object that you can use until you drop it. Views are not updatable.

If a qualified view name is specified, the schema name cannot begin with SYS.

A view operates with the privileges of the owner of the view. See "Using SQL standard authorization" and "Privileges on views, triggers, and constraints" in the Derby Developer's Guide for details.

The view owner automatically gains the SELECT privilege on the view. The SELECT privilege cannot be revoked from the view owner. The database owner automatically gains the SELECT privilege on the view and is able to grant this privilege to other users. The SELECT privilege cannot be revoked from the database owner.

The view owner can only grant the SELECT privilege to other users if the view owner also owns the underlying objects.

If the underlying objects that the view references are not owned by the view owner, the view owner must be granted the appropriate privileges. For example, if the authorization ID user2 attempts to create a view called user2.v2 that references table user1.t1 and function user1.f_abs(), then user2 must have the SELECT privilege on table user1.t1 and the EXECUTE privilege on function user1.f_abs().

The privilege to grant the SELECT privilege cannot be revoked. If a required privilege on one of the underlying objects that the view references is revoked, then the view is dropped.

Syntax

CREATE VIEW view-Name
    [ ( Simple-column-Name [, Simple-column-Name] * ) ]
AS Query [ ORDER BY clause ]
         [ result offset clause ]
         [ fetch first clause ]

A view definition can contain an optional view column list to explicitly name the columns in the view. If there is no column list, the view inherits the column names from the underlying query. All columns in a view must be uniquely named.

Examples

CREATE VIEW SAMP.V1 (COL_SUM, COL_DIFF)
	AS SELECT COMM + BONUS, COMM - BONUS
	FROM SAMP.EMPLOYEE;

CREATE VIEW SAMP.VEMP_RES (RESUME)
	AS VALUES 'Delores M. Quintana', 'Heather A. Nicholls', 'Bruce Adamson';

CREATE VIEW SAMP.PROJ_COMBO 
	(PROJNO, PRENDATE, PRSTAFF, MAJPROJ) 
	AS SELECT PROJNO, PRENDATE, PRSTAFF, MAJPROJ
	FROM SAMP.PROJECT UNION ALL 
SELECT PROJNO, EMSTDATE, EMPTIME, EMPNO 
	FROM SAMP.EMP_ACT 
	WHERE EMPNO IS NOT NULL;

Statement dependency system

View definitions are dependent on the tables and views referenced within the view definition. DML (data manipulation language) statements that contain view references depend on those views, as well as the objects in the view definitions that the views are dependent on. Statements that reference the view depend on indexes the view uses; which index a view uses can change from statement to statement based on how the query is optimized. For example, given:
CREATE TABLE T1 (C1 DOUBLE PRECISION);

CREATE FUNCTION SIN (DATA DOUBLE) 
	RETURNS DOUBLE EXTERNAL NAME 'java.lang.Math.sin'
	LANGUAGE JAVA PARAMETER STYLE JAVA;

CREATE VIEW V1 (C1) AS SELECT SIN(C1) FROM T1;
the following SELECT:
SELECT * FROM V1

is dependent on view V1, table T1, and external scalar function SIN.

Related reference
CREATE FUNCTION statement
CREATE INDEX statement
CREATE PROCEDURE statement
CREATE ROLE statement
CREATE SCHEMA statement
CREATE SEQUENCE statement
CREATE SYNONYM statement
CREATE TABLE statement
CREATE TRIGGER statement
CREATE TYPE statement