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 derived anew.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 operation must target only 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-compatible mode.
Syntax
CREATE [OR REPLACE] VIEW view_name[(column_name_list)]
AS select_stmt
[check_option];
column_name_list:
column_name [, column_name ...]
check_option:
WITH CHECK OPTION
| WITH CASCADED CHECK OPTION
| WITH LOCAL CHECK OPTION
Parameters
| Parameter | Description |
|---|---|
| OR REPLACE | Optional. If specified, the view will be recreated with the new definition if it already exists, effectively replacing the existing view. |
| view_name | Specifies 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 SELECT statement can retrieve columns as simple references to table columns or as expressions involving functions, constants, operators, etc. View column names have the following restrictions:
|
| 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 SELECT statement, see SELECT statement. |
| 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 check_option. |
check_option
WITH CHECK OPTION: A syntax for creating a view that ensures the data inserted or updated meets the view's definition conditions. In MySQL-compatible mode, the WITH CHECK OPTION clause can also be used with the LOCAL or CASCADED keywords to further specify the scope of data checks.
WITH CASCADED CHECK OPTION: Checks the current view and recursively checks all referenced views.WITH LOCAL CHECK OPTION: Checks the current view. Whether to check the referenced views depends on the definitions of those views.
OceanBase Database also supports specifying the WITH CHECK OPTION clause when a subquery is included in the view's filter condition (where_clause).
Notice
When using the WITH CHECK OPTION clause in filter conditions:
- In MySQL-compatible mode, ensure that the creation statement does not result in an "updatable view".
- The
JOINoperation is not supported in view definitions.
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;