A view is a virtual table created from existing tables at the table layer, allowing you to maintain query logic (SQL statements) without needing additional tables that consume storage space. This topic describes the statements for managing views and includes examples of their usage.
Operations
Type | Role | Operation platform |
Create or update a view
This operation creates a new view or updates an existing one by using a query statement.
Limits
A view can reference other views but you must not create a circular reference or reference itself.
Data cannot be written to a view. Operations such as INSERT INTO
or INSERT OVERWRITE
are not permitted.
Syntax
CREATE [OR REPLACE] VIEW [IF NOT EXISTS] <view_name>
[(<col_name> [comment <col_comment>], ...)]
[comment <view_comment>]
AS <select_statement>;
Parameters
Parameter | Required | Description |
Parameter | Required | Description |
OR REPLACE | No | This field must be included to update an existing view. |
IF NOT EXISTS | No | If IF NOT EXISTS is not specified, attempting to create a view that already exists by using CREATE VIEW will result in an exception. In this case, CREATE OR REPLACE VIEW can be used to recreate the view, and the view's permissions remain unchanged after recreation. |
view_name | Yes | The name of the view to be created or updated. |
col_name | Yes | The names of the columns in the view to be created. |
col_comment | No | The comment for the columns in the view to be created. |
view_comment | No | The comment for the view to be created. |
select_statement | Yes | The SELECT query statement that serves as the data source for the view. You must have read permission for the tables referenced in the view. Only one valid SELECT statement is allowed in a view. |
Note
After a view is created or updated, the view may be inaccessible if its referenced table is modified. For example, the referenced table is deleted. You must maintain the mappings between referenced tables and views.
Examples
Rename a view
Syntax
ALTER VIEW <view_name> RENAME TO <new_view_name>;
Parameters
Parameter | Required | Description |
Parameter | Required | Description |
view_name | Yes | The name of the view to be renamed. |
new_view_name | Yes | The new name for the view. An error is returned if a view with the new name already exists. |
Example
ALTER VIEW sale_detail_view RENAME TO market;
Change the owner of a view
Syntax
ALTER VIEW <view_name> CHANGEOWNER TO <new_owner>;
Parameters
Parameter | Required | Description |
Parameter | Required | Description |
view_name | Yes | The name of the view whose owner you want to modify. |
new_owner | Yes | The account of the new owner after modification. |
Example
ALTER VIEW sale_detail_view CHANGEOWNER TO 'ALIYUN$xxx@aliyun.com';
List all standard views in a project
Note
The SHOW VIEWS;
command requires MaxCompute client (odpscmd) V0.43.0 or later.
Syntax
To list only the views in a project, use SHOW VIEWS;
. Alternatively, SHOW TABLES;
lists all tables and views in a project. For more information, see List tables and views in a project.
SHOW VIEWS;
SHOW VIEWS LIKE '<chart>';
Example
Returned result:
ALIYUN$account_name:sale_detail_view
......
Query a view
View the definition information, creation time, project to which the view belongs, and other metadata information.
Drop a view
This command removes an existing view from the database.
Syntax
DROP VIEW [IF EXISTS] <view_name>;
Parameters
Parameter | Required | Description |
Parameter | Required | Description |
IF EXISTS | No | If the view does not exist and IF EXISTS is not specified, an error is returned. |
view_name | Yes | The name of the view to be deleted. |
Example
DROP VIEW IF EXISTS sale_detail_view;