Creates a view definition. A view is a logical structure that refers to rows stored in other tables. Data in a view is not physically stored in the database. You can include in a view definition combinations of rows and columns from other tables and view definitions in the schema. You define a view by specifying a select expression, that: o Names the criteria for selecting the tables, rows, and columns for the view o Specifies a set of columns from those tables When the CREATE VIEW statement executes, SQL adds the view definition to the physical database. If you declared the schema with the PATHNAME argument, the definition is also stored in the repository.
1 – Environment
You can use the CREATE VIEW statement: o In interactive SQL o Embedded in host language programs to be precompiled o As part of a procedure in an SQL module o In dynamic SQL as a statement to be dynamically executed
2 – Format
(B)0[m[1;4mCREATE[m[1m [1;4mVIEW[m[1m qq> <view-name> qqqqqqqqqqqqqqqqqqqqqqqqqk[m [1m lqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj[m [1m mwqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqwqqqqqqqqqqqqqk[m [1m mq> [1;4mSTORED[m[1m [1;4mNAME[m[1m IS <stored-name> qqqj x[m [1mlqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj[m [1mmwqqqqqqqqqqqqqqqqqqqqqqqq>qqqqqqqqqqqqqqqqqqqqqqqqwqk[m [1m mq> ( qwq> <column-name> qwqqqqqqqqqqqqqqwqwq> ) qj x[m [1m x lqqqqqqqqqqqqqqqqj x x x[m [1m x mwqwqqqqqqqqqqqqqqqqqqqqqqqqwqwqj x x[m [1m x x mq> sql-and-dtr-clause qqj x x x[m [1m x mqqqqqqqqqqqqq<qqqqqqqqqqqqqqj x x[m [1m mqqqqqqqqqqqqqqqqq , <qqqqqqqqqqqqqqj x[m [1mlqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj[m [1mmq> [1;4mAS[m[1m select-expr qqqwqqqqqqqqqqqqqqqqqqqqqqqqwqq> [m [1m mq> check-option-clause qj [m [1m [m (B)0[m[1mselect-expr = [m [1m [m [1m [m [1m [m [1mqwqwq> select-clause qqqqqqqqqqqqwqwqqqqqqk[m [1m [m [1m x tq> ( select-expr ) qqqqqqqqqqqu x [m [1mx[m [1m [m [1m [m [1m x mq> [1;4mTABLE[m[1m table-ref qqqqqqqqqqj x x [m [1m [m [1m mqqqqqq select-merge-clause <qqqqqqqj [m [1m x[m [1m lqqqqqqqqqqqqqqqqqqq <qqqqqqqqqqqqqqqqqqqj[m [1m [m [1mmqwqqqqqqqqqqqqqqqqqqqqwqqwqqqqqqqqqqqqqqqqqqwqqwqqqqqqqqqqqqqqqqqqqqwq>[m [1m [m [1m mq> order-by-clause qj[m [1mmq> offset-clause qj [m [1mmq> limit-to-clause qj[m (B)0[m[1msql-and-dtr-clause = [m [1m [m [1mqwq> [1;4mQUERY[m[1m [1;4mHEADER[m[1m IS qw> <quoted-string> wqqqqqqqqqqqqqqqqqqqwq> [m [1m x mqqqqqq / <qqqqqqqqj x [m [1m tq> [1;4mEDIT[m[1m [1;4mSTRING[m[1m IS <quoted-string> qqqqqqqqqqqqqqqqqqqqqqqqqu [m [1m x x [m [1m tq> [1;4mQUERY[m[1m [1;4mNAME[m[1m FOR qwq> DTR qqqqqqqqwq> IS <quoted-string> qu [m [1m x mq> DATATRIEVE qj x [m [1m mq> [1;4mDEFAULT[m[1m [1;4mVALUE[m[1m FOR qwq> DTR qqqqqqqqwq> IS literal[m [1m qqqqj [m [1m mq> DATATRIEVE qj [m [1m [m (B)0[m[1mcheck-option-clause = [m [1m [m [1;4mWITH[m[1m [1;4mCHECK[m[1m [1;4mOPTION[m[1m qqwqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqwqq>[m [1m mq> [1;4mCONSTRAINT[m[1m <check-option-name> j [m [1m [m
3 – Arguments
3.1 – check-option-clause
A constraint that places restrictions on update operations made to a view. The check option clause ensures that any rows that are inserted or updated in a view conform to the definition of the view. Do not specify the WITH CHECK OPTION clause with views that are read-only.
3.2 – column-name
A list of names for the columns of the view. If you omit column names, SQL assigns the names from the columns in the source tables in the select expression. However, you must specify names for all the columns of the view in the following cases: o The select expression generates columns with duplicate names. o The select expression uses statistical functions or arithmetic expressions to create new columns that are not in the source tables.
3.3 – CONSTRAINT
Specify a name for the WITH CHECK OPTION constraint. If you omit the name, SQL creates a name. However, Oracle Rdb recommends that you always name constraints. If you supply a name for the WITH CHECK OPTION constraint, the name must be unique in the schema. The name for the WITH CHECK OPTION constraint is used by the INTEG_FAIL error message when an INSERT or UPDATE statement violates the constraint.
3.4 – select-expr
A select expression that defines which columns and rows of the specified tables SQL includes in the view. The select expression for a nonmultischema database can name only tables in the same schema as the view. A select expression for a multischema database can name a table in any schema in the database; the schema need not be in the same catalog as the view being created. See the Select_Expressions HELP topic for more information on select expressions.
3.5 – sql-and-dtr-clause
Optional SQL and DATATRIEVE formatting clauses. See the DATATRIEVE HELP topic for more information on formatting clauses.
3.6 – STORED_NAME_IS
Specifies a name that Oracle Rdb uses to access a view created in a multischema database. The stored name allows you to access multischema definitions using interfaces, such as Oracle RMU, the Oracle Rdb management utility, that do not recognize multiple schemas in one database. You cannot specify a stored name for a view in a database that does not allow multiple schemas. For more details about stored names, see the User_Supplied_Names HELP topic.
3.7 – view-name
Name of the view definition you want to create. When choosing a name, follow these rules: o Use a name that is unique among all view and table names in the schema. o Use any valid SQL name (see the User_Supplied_Names HELP topic for more information).
4 – Examples
Example 1: Defining a view based on a single table This example shows a view definition that uses three columns from a single table, EMPLOYEES. SQL> CREATE VIEW EMP_NAME cont> AS SELECT cont> FIRST_NAME, cont> MIDDLE_INITIAL, cont> LAST_NAME cont> FROM EMPLOYEES; SQL> -- SQL> -- Now display the rows from the view just created. SQL> SELECT * FROM EMP_NAME; FIRST_NAME MIDDLE_INITIAL LAST_NAME Alvin A Toliver Terry D Smith . . . Example 2: Defining a view that does not allow you to insert or update rows that do not conform to the view's definition This example shows a view definition using the WITH CHECK OPTION clause. SQL> CREATE VIEW ADMN_VIEW cont> AS SELECT * FROM JOB_HISTORY cont> WHERE DEPARTMENT_CODE = 'ADMN' cont> WITH CHECK OPTION CONSTRAINT ADMN_VIEW_CONST; SQL> -- You cannot insert a row that does not SQL> -- conform to the view definition. SQL> -- SQL> INSERT INTO ADMN_VIEW (DEPARTMENT_CODE) VALUES ('MBMN'); %RDB-E-INTEG-FAIL, violation of constraint ADMN_VIEW_CONST- caused operation to fail Example 3: Defining a view based on multiple tables You can also define a view using more than one table. SQL> CREATE VIEW CURRENT_SALARY cont> AS SELECT cont> E.LAST_NAME, cont> E.FIRST_NAME, cont> E.EMPLOYEE_ID, cont> SH.SALARY_START, cont> SH.SALARY_AMOUNT cont> FROM cont> SALARY_HISTORY SH, EMPLOYEES E cont> WHERE cont> SH.EMPLOYEE_ID = E.EMPLOYEE_ID cont> AND cont> SH.SALARY_END IS NULL ; This example defines a view from the EMPLOYEES and SALARY_HISTORY tables. It uses the select expression to: o Choose the columns derived from each table. Because no column names are specified before the select expression, the columns inherit the names from the source tables. o Join the tables and limit the view to current salaries. Example 4: Defining a view with local column names SQL> CREATE VIEW EMP_JOB cont> ( CURRENT_ID, cont> CURRENT_NAME, cont> CURRENT_JOB, cont> SUPERVISOR ) cont> AS SELECT cont> E.EMPLOYEE_ID, cont> E.LAST_NAME, cont> J.JOB_TITLE, cont> JH.SUPERVISOR_ID cont> FROM cont> EMPLOYEES E, cont> JOB_HISTORY JH, cont> JOBS J cont> WHERE cont> E.EMPLOYEE_ID = JH.EMPLOYEE_ID cont> AND cont> JH.JOB_CODE = J.JOB_CODE cont> AND cont> JH.JOB_END IS NULL ; This view definition: o Specifies local names for the columns in the view. o Joins the EMPLOYEES and JOB_HISTORY tables. This join links rows in the EMPLOYEES table to rows in the JOB_HISTORY table. o Joins the JOB_HISTORY and JOBS tables. This join lets the view contain job titles instead of job codes. o Uses the JH.JOB_END IS NULL expression. This clause specifies that only the current JOB_HISTORY rows, where the JOB_END column is null, should be included in the view. The following query uses the view defined in the previous example: EXEC SQL DECLARE X CURSOR FOR SELECT CURRENT_ID, CURRENT_NAME, CURRENT_JOB, SUPERVISOR FROM EMP_JOB END-EXEC EXEC SQL OPEN X END-EXEC PERFORM WHILE SQLCODE NOT = 0 EXEC SQL FETCH X INTO :ID, :NAME, :JOB, :SUPER END-EXEC END PERFORM EXEC SQL CLOSE X END-EXEC Example 5: Defining a view with a calculated column This example shows a view definition that derives a column through a calculation based on a column in an base table. SQL> CREATE VIEW SS_DEDUCTION cont> ( IDENT, cont> SALARY, cont> SS_AMOUNT ) cont> AS SELECT cont> E.EMPLOYEE_ID, cont> SH.SALARY_AMOUNT, cont> SH.SALARY_AMOUNT * 0.065 cont> FROM cont> SALARY_HISTORY SH, EMPLOYEES E cont> WHERE cont> SH.EMPLOYEE_ID = E.EMPLOYEE_ID cont> AND cont> SH.SALARY_END IS NULL ; Each time the view column SS_AMOUNT is selected, it computes a new value from the SALARY_AMOUNT column of the SALARY_HISTORY table. Example 6: Defining a view dependent on another view This example creates a view, DEPENDENT_VIEW, that refers to the CURRENT_JOB view in its definition to include current job information for employees in the engineering department. SQL> CREATE VIEW DEPENDENT_VIEW cont> AS SELECT * FROM CURRENT_JOB cont> WHERE DEPARTMENT_CODE = 'ENG';