Purpose
This statement is used to create a view.
Limitations and considerations
If the OR REPLACE clause is specified, the existing view will be replaced.
A view is not stored as a table in the database. Instead, it is derived from the SELECT statement specified in the CREATE VIEW statement. Each time the view is used, it is derived.
If the FROM clause references two or more tables or views, the view is referred to as 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 restricted by the WITH READ ONLY clause.
To be updatable, a view must meet several conditions. One common rule is that for a join view, an INSERT, UPDATE, or DELETE operation can only modify 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-compatible mode.
Syntax
CREATE [OR REPLACE] [[NO] FORCE] VIEW view_name [(column_name_list)]
AS select_stmt
[view_check_option];
select_stmt:
SELECT column_name, [--string]
[, column_name, [--string] ...]
FROM from_list
[WHERE condition]
column_name_list:
column_name [, column_name ...]
view_check_option:
WITH READ ONLY
| WITH CHECK OPTION
Parameters
| Parameter | Description |
|---|---|
| OR REPLACE | Indicates whether to recreate the view with the new definition if the view already exists. |
| [NO] FORCE | If you want to create the view without considering whether the view has base tables or referenced objects, or whether the owner of the schema where the view is located has the necessary privileges, specify FORCE. If you want to create the view only when the base tables exist and the owner of the schema where the view is located has the necessary privileges, specify NOFORCE. NOFORCE is the default. |
| view_name | The name of the view. |
| column_name_list | The view must have unique column names, just like base tables. By default, the column names retrieved by the SELECT statement will be used as the view column names. To define specific names for the view columns, you can use the optional column_name_list clause, with IDs separated by commas. The number of names in column_name_list must be equal to the number of columns retrieved by the SELECT statement. The columns retrieved by the SELECT statement can be simple references to table columns. They can also be expressions involving functions, constants, operators, etc. |
| select_stmt | A SELECT statement that defines the view. The statement can select data from base tables or other views. For more information about SELECT statements, see SIMPLE SELECT. |
| --string | Optional. Used to add comments to the view. Comments are stored in the view definition.
Note
|
| view_check_option | Optional. Used to specify additional properties of the view. For more information, see view_check_option below. |
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 not for DML operations (INSERT,UPDATE,DELETE). This means that you cannot insert, update, or delete data 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. This means that you can perform DML operations through the view, but the data must meet the view's definition conditions.Note
In Oracle-compatible mode, the
WITH CHECK OPTIONsyntax does not support specifyingLOCALorCASCADED. The default isCASCADED.OceanBase Database also supports specifying the
WITH CHECK OPTIONclause when the view filter condition (where_clause) contains a subquery.Notice
When using the
WITH CHECK OPTIONclause in the filter condition, the view definition does not support theJOINoperation.
Examples
Create the test tables tbl1 and tbl2.
obclient> CREATE TABLE tbl1 (col1 INT, col2 VARCHAR(50));
obclient> CREATE TABLE tbl2 (col1 INT, col2 INT);
Select the
col1andcol2columns from tabletbl1to create the viewv1_t1.obclient> CREATE OR REPLACE FORCE VIEW v1_t1(vcol1, vcol2) AS SELECT col1, col2 FROM tbl1;Create the view
v2_t2based on thecol1andcol2columns of tabletbl2, which contains all data that satisfies the conditiontbl2.col2 > 1.obclient> CREATE VIEW v2_t2 AS SELECT * FROM tbl2 WHERE tbl2.col2 > 1 WITH CHECK OPTION;Create the view
v3_t3based on thecol1andcol2columns of tabletbl2, 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 view comments by using
SHOW CREATE VIEWorDBMS_METADATA.GET_DDL.Query the view comments 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 view comments 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
Specify the
WITH CHECK OPTIONclause after the subquery in the filter condition.obclient> CREATE OR REPLACE VIEW v4_t1 AS SELECT * FROM tbl1 t1 WHERE EXISTS (SELECT 1 FROM tbl2 t2 WHERE t2.col1 = t1.col1) WITH CHECK OPTION;