Purpose
This statement is used to create a view.
Limitations and considerations
If the
OR REPLACEclause is specified, the statement can replace an existing view.A view is not stored as a table in the database. It is derived from the
SELECTstatement specified in theCREATE VIEWstatement. Each time the view is used, it is derived.If the
FROMclause references two or more tables or views, the view is a join view. An updatable join view, also known as a modifiable join view, involves two or more base tables or views and allows DML operations. Updatable views are not limited by theWITH READ ONLYclause.To be updatable, a view must meet several conditions. For example, one common rule is that for a join view, an
INSERT,UPDATE, orDELETEoperation can modify only one base table at a time.
Privilege requirements
To execute the CREATE VIEW statement, the current user must have the CREATE VIEW privilege. For more information about privileges in OceanBase Database, see Privilege classification in Oracle mode.
Syntax
CREATE [OR REPLACE] [{NO FORCE} | FORCE] VIEW view_name [(column_name_list)]
AS select_stmt
[view_check_option];
column_name_list:
column_name [, column_name ...]
view_check_option:
WITH READ ONLY
| WITH CHECK OPTION
Parameters
| Parameter | Description |
|---|---|
| OR REPLACE | Optional. If specified, the view will be created with the new definition if the view already exists. |
| {NO FORCE} | FORCE | Optional. Controls whether the view creation is enforced.
|
| view_name | The name of the view. |
| column_name_list | Optional. The list of column names for the view. If not specified, the column names retrieved by the SELECT statement will be used as the view column names. The columns retrieved by the SELECT statement can be simple references to table columns or expressions involving functions, constants, operators, etc. The view column names have the following limitations:
|
| select_stmt | The SELECT statement used to define the view. It specifies the view's definition, which can be queried from the base table or other views. For specific structures and options of the query statement, see SELECT statement. |
| view_check_option | Optional. Specifies additional properties of the view. For more information, see view_check_option. |
view_check_option
WITH READ ONLY: An option for creating or replacing a view. It specifies that the view can only be used for reading data and cannot be used for DML operations (INSERT,UPDATE,DELETE). That is, data cannot be inserted, updated, or deleted through the view.WITH CHECK OPTION: A syntax for creating a view. It ensures that the data inserted or updated meets the view's definition conditions. That is, DML operations are allowed through the view, but the data after the operation must meet the view's definition conditions.Note
In Oracle mode, the
WITH CHECK OPTIONsyntax does not support specifyingLOCALorCASCADED. The default value isCASCADED.
Example
Create the test tables tbl1 and tbl2.
obclient> CREATE TABLE tbl1 (col1 INT, col2 VARCHAR(50));
obclient> CREATE TABLE tbl2 (col1 INT, col2 INT);
Create the view
v1_t1by selecting thecol1andcol2columns from thetbl1table.obclient> CREATE OR REPLACE FORCE VIEW v1_t1(vcol1, vcol2) AS SELECT col1, col2 FROM tbl1;Create the view
v2_t2by selecting thecol1andcol2columns from thetbl2table, and include all data that satisfies the conditiontbl2.col2 > 1in the view.obclient> CREATE VIEW v2_t2 AS SELECT * FROM tbl2 WHERE tbl2.col2 > 1 WITH CHECK OPTION;Create the view
v3_t3by selecting thecol1andcol2columns from thetbl2table, and add comments to the viewv3_t3.Create the view
v3_t3.obclient> CREATE VIEW v3_t3 AS SELECT col1, --test.col1 col2 --test.col2 FROM tbl2;Query the comments of the view by using
SHOW CREATE VIEWorDBMS_METADATA.GET_DDL.Query the comments of the view by using
SHOW CREATE VIEW.obclient> SHOW CREATE VIEW v3_t3;The query result is as follows:
+-------+------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+ | VIEW | CREATE VIEW | CHARACTER_SET_CLIENT | COLLATION_CONNECTION | +-------+------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+ | V3_T3 | CREATE VIEW "V3_T3" ("COL1", "COL2") AS SELECT col1, --test.col1 col2 --test.col2 FROM tbl2 | utf8mb4 | utf8mb4_bin | +-------+------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+ 1 row in setQuery the comments of the view by using
DBMS_METADATA.GET_DDL.obclient> SELECT DBMS_METADATA.GET_DDL('VIEW', 'V3_T3', 'SYS') FROM DUAL;The query result is as follows:
+-------+------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+ | VIEW | CREATE VIEW | CHARACTER_SET_CLIENT | COLLATION_CONNECTION | +-------+------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+ | V3_T3 | CREATE VIEW "V3_T3" ("COL1", "COL2") AS SELECT col1, --test.col1 col2 --test.col2 FROM tbl2 | utf8mb4 | utf8mb4_bin | +-------+------------------------------------------------------------------------------------------------------------------------------------------+----------------------+----------------------+ 1 row in set (0.028 sec) obclient(SYS@oracle001)[SYS]> SELECT DBMS_METADATA.GET_DDL('VIEW', 'V3_T3', 'SYS') FROM DUAL; +------------------------------------------------------------------------------------------------------------------------------------------+ | DBMS_METADATA.GET_DDL('VIEW','V3_T3','SYS') | +------------------------------------------------------------------------------------------------------------------------------------------+ | CREATE VIEW "V3_T3" ("COL1", "COL2") AS SELECT col1, --test.col1 col2 --test.col2 FROM tbl2 | +------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set