ALTER VIEW

描述

ALTER VIEW 语句可以修改与视图相关的元数据。它可以更改视图的定义,将视图的名称更改为其他名称,通过设置 TBLPROPERTIES 来设置和取消设置视图的元数据。

重命名视图

重命名现有视图。如果新视图名称已存在于源数据库中,则会抛出 TableAlreadyExistsException 异常。此操作不支持跨数据库移动视图。

如果视图被缓存,此命令会清除视图及其所有引用该视图的依赖项的缓存数据。视图的缓存将在下次访问视图时惰性填充。该命令会将视图的依赖项保持为未缓存状态。

语法

ALTER VIEW view_identifier RENAME TO view_identifier

参数

设置视图属性

设置现有视图的一个或多个属性。属性是键值对。如果属性键已存在,则其值将被新值替换。如果属性键不存在,则将键值对添加到属性中。

语法

ALTER VIEW view_identifier SET TBLPROPERTIES ( property_key = property_val [ , ... ] )

参数

取消设置视图属性

删除现有视图的一个或多个属性。如果指定的键不存在,则会抛出异常。使用 IF EXISTS 以避免异常。

语法

ALTER VIEW view_identifier UNSET TBLPROPERTIES [ IF EXISTS ]  ( property_key [ , ... ] )

参数

ALTER View AS SELECT

ALTER VIEW view_identifier AS SELECT 语句更改视图的定义。SELECT 语句必须有效,并且 view_identifier 必须存在。

语法

ALTER VIEW view_identifier AS select_statement

请注意,ALTER VIEW 语句不支持 SET SERDESET SERDEPROPERTIES 属性。

参数

ALTER View WITH SCHEMA

更改视图的模式绑定行为。

如果视图被缓存,此命令会清除视图及其所有引用该视图的依赖项的缓存数据。视图的缓存将在下次访问视图时惰性填充。该命令会将视图的依赖项保持为未缓存状态。

此语句不支持 TEMPORARY 视图。

语法

ALTER VIEW view_identifier WITH SCHEMA { BINDING | COMPENSATION | [ TYPE ] EVOLUTION }

参数

示例

-- Rename only changes the view name.
-- The source and target databases of the view have to be the same.
-- Use qualified or unqualified name for the source and target view.
ALTER VIEW tempdb1.v1 RENAME TO tempdb1.v2;

-- Verify that the new view is created.
DESCRIBE TABLE EXTENDED tempdb1.v2;
+----------------------------+----------+-------+
|                    col_name|data_type |comment|
+----------------------------+----------+-------+
|                          c1|       int|   null|
|                          c2|    string|   null|
|                            |          |       |
|# Detailed Table Information|          |       |
|                    Database|   tempdb1|       |
|                       Table|        v2|       |
+----------------------------+----------+-------+

-- Before ALTER VIEW SET TBLPROPERTIES
DESC TABLE EXTENDED tempdb1.v2;
+----------------------------+----------+-------+
|                    col_name| data_type|comment|
+----------------------------+----------+-------+
|                          c1|       int|   null|
|                          c2|    string|   null|
|                            |          |       |
|# Detailed Table Information|          |       |
|                    Database|   tempdb1|       |
|                       Table|        v2|       |
|            Table Properties|    [....]|       |
+----------------------------+----------+-------+

-- Set properties in TBLPROPERTIES
ALTER VIEW tempdb1.v2 SET TBLPROPERTIES ('created.by.user' = "John", 'created.date' = '01-01-2001' );

-- Use `DESCRIBE TABLE EXTENDED tempdb1.v2` to verify
DESC TABLE EXTENDED tempdb1.v2;
+----------------------------+-----------------------------------------------------+-------+
|                    col_name|                                            data_type|comment|
+----------------------------+-----------------------------------------------------+-------+
|                          c1|                                                  int|   null|
|                          c2|                                               string|   null|
|                            |                                                     |       |
|# Detailed Table Information|                                                     |       |
|                    Database|                                              tempdb1|       |
|                       Table|                                                   v2|       |
|            Table Properties|[created.by.user=John, created.date=01-01-2001, ....]|       |
+----------------------------+-----------------------------------------------------+-------+

-- Remove the key `created.by.user` and `created.date` from `TBLPROPERTIES`
ALTER VIEW tempdb1.v2 UNSET TBLPROPERTIES ('created.by.user', 'created.date');

--Use `DESC TABLE EXTENDED tempdb1.v2` to verify the changes
DESC TABLE EXTENDED tempdb1.v2;
+----------------------------+----------+-------+
|                    col_name| data_type|comment|
+----------------------------+----------+-------+
|                          c1|       int|   null|
|                          c2|    string|   null|
|                            |          |       |
|# Detailed Table Information|          |       |
|                    Database|   tempdb1|       |
|                       Table|        v2|       |
|            Table Properties|    [....]|       |
+----------------------------+----------+-------+

-- Change the view definition
ALTER VIEW tempdb1.v2 AS SELECT * FROM tempdb1.v1;

-- Use `DESC TABLE EXTENDED` to verify
DESC TABLE EXTENDED tempdb1.v2;
+----------------------------+---------------------------+-------+
|                    col_name|                  data_type|comment|
+----------------------------+---------------------------+-------+
|                          c1|                        int|   null|
|                          c2|                     string|   null|
|                            |                           |       |
|# Detailed Table Information|                           |       |
|                    Database|                    tempdb1|       |
|                       Table|                         v2|       |
|                        Type|                       VIEW|       |
|                   View Text|   select * from tempdb1.v1|       |
|          View Original Text|   select * from tempdb1.v1|       |
+----------------------------+---------------------------+-------+

CREATE OR REPLACE VIEW open_orders AS SELECT * FROM orders WHERE status = 'open';
ALTER VIEW open_orders WITH SCHEMA EVOLUTION;
DESC TABLE EXTENDED open_orders;
+----------------------------+---------------------------+-------+
|                    col_name|                  data_type|comment|
+----------------------------+---------------------------+-------+
|                    order_no|                        int|   null|
|                  order_date|                       date|   null|
|                            |                           |       |
|# Detailed Table Information|                           |       |
|                    Database|                       mydb|       |
|                       Table|                open_orders|       |
|                        Type|                       VIEW|       |
|                   View Text|       select * from orders|       |
|          View Original Text|       select * from orders|       |
|          View Schema Mode  |                  EVOLUTION|       |    
+----------------------------+---------------------------+-------+