ALTER VIEW

説明

ALTER VIEW ステートメントは、ビューに関連付けられたメタデータを変更できます。ビューの定義を変更したり、ビューの名前を別の名前に変更したり、TBLPROPERTIES を設定することでビューのメタデータを設定および設定解除したりできます。

ビューの名前変更

既存のビューの名前を変更します。新しいビュー名がソースデータベースに既に存在する場合、TableAlreadyExistsException がスローされます。この操作では、データベース間でビューを移動することはできません。

ビューがキャッシュされている場合、コマンドはビューとそのビューを参照するすべての依存関係のキャッシュされたデータをクリアします。ビューのキャッシュは、次回ビューにアクセスしたときに遅延的に満たされます。コマンドは、ビューの依存関係をキャッシュされていないままにします。

構文

ALTER VIEW view_identifier RENAME TO view_identifier

パラメータ

ビューのプロパティの設定

既存のビューの 1 つ以上のプロパティを設定します。プロパティはキーと値のペアです。プロパティのキーが存在する場合、値は新しい値に置き換えられます。プロパティのキーが存在しない場合、キーと値のペアがプロパティに追加されます。

構文

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

パラメータ

ビューのプロパティの設定解除

既存のビューの 1 つ以上のプロパティを削除します。指定されたキーが存在しない場合、例外がスローされます。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 SERDE プロパティまたは SET SERDEPROPERTIES プロパティをサポートしていないことに注意してください。

パラメータ

-- 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|       |
+----------------------------+---------------------------+-------+