All Products
Search
Document Center

MaxCompute:View-related operations

Last Updated:Nov 18, 2024

A view is a virtual table that is created based on existing tables. A view corresponds to one or more tables. You can retain the query logic (SQL statements) without the need to create additional tables to occupy storage space by using views. This topic describes the statements that you can execute to manage views. This topic also provides examples on how to use the statements.

The following table describes view-related operations.

Operation

Description

Role

Operation platform

Create or update a view

Creates a view or updates an existing view based on a query statement.

Users who have the CreateTable permission on a project

You can execute the statements that are described in this topic on the following platforms:

Rename a view

Renames an existing view.

Users who have the Alter permission on tables

Query a view

Views the information of an existing view.

Users who have the Describe permission to read the metadata of a table

Drop a view

Drops an existing view.

Users who have the Drop permission on tables

Change the owner of a view

Changes the owner of an existing view.

Users who have the Alter permission on tables

Create or update a view

Creates a view or updates an existing view based on a query statement.

  • Limits

    • A view can reference other views but cannot reference itself. Circular reference is not supported.

    • You are not allowed to write data to a view. For example, INSERT INTO or INSERT OVERWRITE cannot be executed on a view.

  • Syntax

    CREATE [OR REPLACE] VIEW [IF NOT EXISTS] <view_name>
        [(<col_name> [comment <col_comment>], ...)]
        [comment <view_comment>]
        AS <select_statement>;
  • Parameters

    • OR REPLACE: optional. This parameter is required when you want to update a view.

    • IF NOT EXISTS: optional. If the CREATE VIEW statement is executed without if not exists and the view that you want to create already exists, an error is returned. In this case, you can execute the CREATE OR REPLACE VIEW statement to recreate the view. The permissions on the view remain unchanged after the view is recreated.

    • view_name: required. The name of the view that you want to create or update.

    • col_name: required. The names of the columns in the view that you want to create.

    • col_comment: optional. The column comments of the view that you want to create.

    • view_comment: optional. The comment of the view that you want to create.

    • select_statement: required. The SELECT clause that provides the data source of the view. You must have read permissions on the table that the view references. A view can contain only one valid SELECT statement.

      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

    • Example 1: Create the sale_detail_view view based on the sale_detail table.

      CREATE VIEW IF NOT EXISTS sale_detail_view 
      (store_name, customer_id, price, sale_date, region) 
      comment 'a view for table sale_detail' 
      AS SELECT * FROM sale_detail;
    • Example 2: Update the sale_detail_view view based on the sale_detail table.

      CREATE OR REPLACE VIEW IF NOT EXISTS sale_detail_view 
      (store_name, customer_id, price)
      comment 'a view for table sale_detail'
      AS SELECT shop_name, customer_id, total_price FROM sale_detail;

Rename a view

Renames an existing view.

  • Syntax

    ALTER VIEW <view_name> RENAME TO <new_view_name>;
  • Parameters

    • view_name: required. The name of the view that you want to rename.

    • new_view_name: required. The new name of the view. If a view with the same name already exists, an error is returned.

  • Example

    -- Rename the sale_detail_view view as market. 
    ALTER VIEW sale_detail_view RENAME TO market;

Query a view

For more information, see View the information about tables or views.

Drop a view

Drops an existing view.

  • Syntax

    DROP VIEW [IF EXISTS] <view_name>;
  • Parameters

    • IF EXISTS: optional. If you do not specify the if exists parameter and the view that you want to drop does not exist, an error is returned.

    • view_name: required. The name of the view that you want to drop.

  • Example

    -- Drop the sale_detail_view view. 
    DROP VIEW IF EXISTS sale_detail_view;

Change the owner of a view

Changes the owner of an existing view.

  • Syntax

    ALTER VIEW <view_name> CHANGEOWNER TO <new_owner>;
  • Parameters

    • view_name: required. The name of the view whose owner you want to change.

    • new_owner: required. The new owner of the view.

  • Example

    -- Change the owner of the sale_detail_view view to the Alibaba Cloud account ALIYUN$xxx@aliyun.com. 
    ALTER VIEW sale_detail_view CHANGEOWNER TO 'ALIYUN$xxx@aliyun.com';