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 two or more tables or views are referenced in the
FROMclause, 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 types in MySQL 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 is recreated with the new definition if it already exists. |
| 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 column names. The columns retrieved by the SELECT statement can be simple references to table columns or 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 query 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 details on the check scope, see check_option. |
check_option
WITH CHECK OPTION: A syntax for creating a view that ensures the inserted or updated data meets the view's definition conditions. In MySQL mode, the WITH CHECK OPTION clause can also be used with the LOCAL/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.
Examples
Create a table named
test_tbl1.CREATE TABLE test_tbl1 (col1 INT, col2 INT);Create a view named
v1based on thetest_tbl1table.CREATE VIEW v1 AS SELECT * FROM test_tbl1 WHERE col2 < 2 WITH CHECK OPTION;Create a view named
v2based on thev1view using theLOCAL CHECK OPTIONsyntax.CREATE VIEW v2 AS SELECT * FROM v1 WHERE col2 > 0 WITH LOCAL CHECK OPTION;Create a view named
v3based on thev1view using theCASCADED CHECK OPTIONsyntax.CREATE VIEW v3 AS SELECT * FROM v1 WHERE col2 > 0 WITH CASCADED CHECK OPTION;