ALTER VIEW
說明
ALTER VIEW
陳述式可以變更與檢視相關的元資料。它可以變更檢視的定義、將檢視名稱變更為不同的名稱、透過設定 TBLPROPERTIES
來設定和取消設定檢視的元資料。
重新命名檢視
重新命名現有的檢視。如果新的檢視名稱已存在於來源資料庫中,則會擲回 TableAlreadyExistsException
。此操作不支援跨資料庫移動檢視。
如果檢視已快取,則此指令會清除檢視的快取資料及其所有參考它的相依項。下次存取檢視時,檢視的快取會延遲填滿。此指令會讓檢視的相依項保持未快取狀態。
語法
ALTER VIEW view_identifier RENAME TO view_identifier
參數
-
view_identifier
指定檢視名稱,可以選擇使用資料庫名稱限定。
語法:
[ database_name. ] view_name
設定檢視屬性
設定現有檢視的一個或多個屬性。這些屬性是關鍵值配對。如果屬性的金鑰存在,則其值會被新的值取代。如果屬性的金鑰不存在,則關鍵值配對會新增到屬性中。
語法
ALTER VIEW view_identifier SET TBLPROPERTIES ( property_key = property_val [ , ... ] )
參數
-
view_identifier
指定檢視名稱,可以選擇使用資料庫名稱限定。
語法:
[ database_name. ] view_name
-
property_key
指定屬性金鑰。金鑰可以包含多個部分,並以句點分隔。
語法:
[ key_part1 ] [ .key_part2 ] [ ... ]
取消設定檢視屬性
刪除現有檢視的一個或多個屬性。如果指定的金鑰不存在,則會擲回例外。使用 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| |
+----------------------------+---------------------------+-------+