ALTER VIEW
説明
ALTER VIEW
ステートメントは、ビューに関連付けられたメタデータを変更できます。ビューの定義を変更したり、ビューの名前を別の名前に変更したり、TBLPROPERTIES
を設定することでビューのメタデータを設定および設定解除したりできます。
ビューの名前変更
既存のビューの名前を変更します。新しいビュー名がソースデータベースに既に存在する場合、TableAlreadyExistsException
がスローされます。この操作では、データベース間でビューを移動することはできません。
ビューがキャッシュされている場合、コマンドはビューとそのビューを参照するすべての依存関係のキャッシュされたデータをクリアします。ビューのキャッシュは、次回ビューにアクセスしたときに遅延的に満たされます。コマンドは、ビューの依存関係をキャッシュされていないままにします。
構文
ALTER VIEW view_identifier RENAME TO view_identifier
パラメータ
-
view_identifier
ビュー名を指定します。必要に応じてデータベース名で修飾できます。
構文:
[ database_name. ] view_name
ビューのプロパティの設定
既存のビューの 1 つ以上のプロパティを設定します。プロパティはキーと値のペアです。プロパティのキーが存在する場合、値は新しい値に置き換えられます。プロパティのキーが存在しない場合、キーと値のペアがプロパティに追加されます。
構文
ALTER VIEW view_identifier SET TBLPROPERTIES ( property_key = property_val [ , ... ] )
パラメータ
-
view_identifier
ビュー名を指定します。必要に応じてデータベース名で修飾できます。
構文:
[ database_name. ] view_name
-
property_key
プロパティキーを指定します。キーは、ドットで区切られた複数の部分で構成される場合があります。
構文:
[ key_part1 ] [ .key_part2 ] [ ... ]
ビューのプロパティの設定解除
既存のビューの 1 つ以上のプロパティを削除します。指定されたキーが存在しない場合、例外がスローされます。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| |
+----------------------------+---------------------------+-------+