CREATE VIEW
Overview
CREATE VIEW is used to create a virtual table based on a SELECT query. A view does not store data. Instead, it dynamically executes the underlying query each time it is accessed. Views can be used to simplify complex queries, provide a logical data abstraction layer, and implement access control.
Application Scenarios
- Simplified query: Complex queries such as multi-table JOIN and aggregation are encapsulated into views, and the view names are directly referenced in subsequent SQL statements.
- Data masking: Creates a view that contains only public fields and hides sensitive columns.
- Logical layering: In the ETL process, defines intermediate processing logic using views to improve SQL readability and maintainability.
Notes and Constraints
- AI DataLake does not support the CREATE TEMPORARY VIEW syntax.
- A view is read-only. Data cannot be inserted into or updated in a view.
- The table or view referenced in the view definition must exist. Otherwise, the view fails to be created.
- Views do not support table attribute clauses such as PARTITIONED BY and WITH.
- When the underlying table structure changes (for example, a column is deleted), the view may become invalid.
Syntax Structure
CREATE VIEW [IF NOT EXISTS] [catalog_name.][db_name.]view_name [( columnName [, columnName ]* )] [COMMENT view_comment] AS query_expression
Syntax Description
| Parameter | Mandatory | Description |
|---|---|---|
| IF NOT EXISTS | No | Specifies that no error is reported when a view with the same name already exists. If this parameter is not specified, an exception is thrown if a view with the same name already exists. |
| view_name | Yes | Specifies the view name, which must be unique in the database. You can specify the catalog_name and db_name prefixes. |
| columnName | No | Specifies the list of column names in the customized view. If this parameter is omitted, the column names in the query result are used. The number of column names must be the same as that of columns in the query result. |
| COMMENT | No | Specifies the comment of the view, which helps understand the purpose and service meaning of the view. |
| query_expression | Yes | Specifies the SELECT query statement that defines the view content. The statement can reference tables, other views, and functions. |
Usage Example
Example 1: Create a view.
-- Create a DataGen source table. CREATE TABLE datagen_source ( user_id BIGINT, product_id BIGINT, amount DECIMAL(10, 2), category STRING, ts TIMESTAMP(3), WATERMARK FOR ts AS ts - INTERVAL '5' SECOND ) WITH ( 'connector' = 'datagen', 'rows-per-second' = '100', 'fields.user_id.kind' = 'sequence', 'fields.user_id.start' = '1', 'fields.user_id.end' = '10000', 'fields.product_id.kind' = 'sequence', 'fields.product_id.start' = '100', 'fields.product_id.end' = '200', 'fields.category.length' = '6' ); -- Create the Print result table. CREATE TABLE print_sink ( user_id BIGINT, total_amount DECIMAL(10, 2) ) WITH ( 'connector' = 'print', 'print-identifier' = 'view_output' ); -- Create a view to aggregate amounts by user. CREATE VIEW IF NOT EXISTS user_amount_view AS SELECT user_id, SUM(amount) AS total_amount FROM datagen_source GROUP BY user_id; -- Submit the job. INSERT INTO print_sink SELECT user_id, total_amount FROM user_amount_view;
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot