Purpose
This statement is used to create a view.
Limitations and considerations
A view is not stored as a table in the database. Instead, it is derived from the
SELECTstatement specified in theCREATE VIEWstatement. Each time the view is used, it is re-derived.If the
FROMclause references two or more tables or views, the view is called 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 theWITH READ ONLYclause.To ensure the updatable nature of a view, a series of specific conditions must be met. One universally applicable rule is that when performing
INSERT,UPDATE, orDELETEoperations on a join view, the operations can only modify a single base table.
Privilege requirements
To execute the CREATE VIEW statement, the current user must have the CREATE VIEW privilege. For more information about OceanBase Database privileges, see Privilege classification in MySQL mode.
Syntax
CREATE [OR REPLACE] VIEW view_name [(column_name_list)]
AS select_stmt
[view_check_option];
column_name_list:
column_name [, column_name ...]
view_check_option:
WITH [LOCAL | CASCADED] CHECK OPTION
Parameters
| Parameter | Description |
|---|---|
| OR REPLACE | Optional. If specified, the view will be recreated with the new definition if the view name already exists. This effectively replaces the existing view. |
| view_name | The name of the view. |
| column_name_list | Optional. Specifies 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's column names. The columns retrieved by the SELECT statement can be simple references to table columns or expressions involving functions, constants, operators, etc. The following restrictions apply to view column names:
|
| select_stmt | The SELECT statement used to define the view. It specifies the view's definition, allowing selection from base tables or other views. For more information about the structure and options of the query statement, see SELECT statement. |
| view_check_option | Optional. Specifies the check scope for the view, used to restrict insert and update operations on the view. For more information about the check scope, see view_check_option. |
view_check_option
WITH CHECK OPTION is a syntax used to create a view, ensuring that inserted or updated data meets the view's definition conditions. The WITH CHECK OPTION clause can also be used with the LOCAL or CASCADED keywords to further specify the scope of data checks. The difference between these two keywords lies in the scope of data checks:
LOCAL: Checks only the current view. Whether to check the referenced views depends on their definitions.CASCADED: The default value. Checks the current view and recursively checks all referenced views.
Starting from V4.2.5 BP7, the WITH CHECK OPTION clause is supported when the view filter condition (where_clause) includes subqueries.
Notice
When using the WITH CHECK OPTION clause in the filter condition:
- In MySQL mode, the creation statement must not create an "updatable view".
- The
JOINoperation is not supported in the view definition.
Examples
Create the test tables test_tbl1 and test_tbl2.
obclient> CREATE TABLE test_tbl1 (col1 INT, col2 VARCHAR(50), col3 INT);
obclient> CREATE TABLE test_tbl2 (col1 INT, col2 VARCHAR(50));
Create a view
v1_t1by selecting thecol1andcol2columns from tabletest_tbl1.obclient> CREATE OR REPLACE VIEW v1_t1 AS SELECT col1, col2 FROM test_tbl1;Create a view
v2_t1using theWITH LOCAL CHECK OPTIONclause.obclient> CREATE OR REPLACE VIEW v2_t1 AS SELECT * FROM test_tbl1 WHERE col1 > 0 WITH LOCAL CHECK OPTION;Create a view
v3_t1using theWITH CASCADED CHECK OPTIONclause.obclient> CREATE OR REPLACE VIEW v3_t1 AS SELECT * FROM test_tbl1 WHERE col1 > 1 WITH CASCADED CHECK OPTION;Create a view
v4_t1using theWITH CHECK OPTIONclause.obclient> CREATE OR REPLACE VIEW v4_t1 AS SELECT * FROM test_tbl1 WHERE col1 > 10 WITH CHECK OPTION;Specify the
WITH CHECK OPTIONclause after the filter condition subquery.obclient> CREATE OR REPLACE VIEW v5_t1_t2 AS SELECT * FROM test_tbl1 t1 WHERE EXISTS (SELECT 1 FROM test_tbl2 t2 WHERE t2.col1 = t1.col1) WITH CHECK OPTION;