ALTER VIEW
描述
ALTER VIEW
语句可以更改与视图关联的元数据。 它可以更改视图的定义,将视图的名称更改为不同的名称,并通过设置 TBLPROPERTIES
来设置和取消设置视图的元数据。
RENAME View
重命名现有视图。 如果新的视图名称已存在于源数据库中,则会抛出 TableAlreadyExistsException
。 此操作不支持跨数据库移动视图。
如果视图被缓存,该命令将清除视图及其所有引用它的依赖项的缓存数据。 当下次访问该视图时,将延迟填充视图的缓存。 该命令将视图的依赖项保留为未缓存。
语法
ALTER VIEW view_identifier RENAME TO view_identifier
参数
-
view_identifier
指定一个视图名称,可以选择使用数据库名称进行限定。
语法:
[ database_name. ] view_name
SET View Properties
设置现有视图的一个或多个属性。 这些属性是键值对。 如果属性的键存在,则值将被新值替换。 如果属性的键不存在,则键值对将被添加到属性中。
语法
ALTER VIEW view_identifier SET TBLPROPERTIES ( property_key = property_val [ , ... ] )
参数
-
view_identifier
指定一个视图名称,可以选择使用数据库名称进行限定。
语法:
[ database_name. ] view_name
-
property_key
指定属性键。 键可以包含多个部分,并用点分隔。
语法:
[ key_part1 ] [ .key_part2 ] [ ... ]
UNSET View Properties
删除现有视图的一个或多个属性。 如果指定的键不存在,则会引发异常。 使用 IF EXISTS
以避免异常。
语法
ALTER VIEW view_identifier UNSET TBLPROPERTIES [ IF EXISTS ] ( property_key [ , ... ] )
参数
-
view_identifier
指定一个视图名称,可以选择使用数据库名称进行限定。
语法:
[ database_name. ] view_name
-
property_key
指定属性键。 键可以包含多个部分,并用点分隔。
语法:
[ key_part1 ] [ .key_part2 ] [ ... ]
ALTER View AS SELECT
ALTER VIEW view_identifier AS SELECT
语句更改视图的定义。 SELECT
语句必须有效,并且 view_identifier
必须存在。
语法
ALTER VIEW view_identifier AS select_statement
请注意,ALTER VIEW
语句不支持 SET SERDE
或 SET SERDEPROPERTIES
属性。
参数
-
view_identifier
指定一个视图名称,可以选择使用数据库名称进行限定。
语法:
[ database_name. ] view_name
-
select_statement
指定视图的定义。 有关详细信息,请检查 select_statement。
示例
-- 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| |
+----------------------------+---------------------------+-------+