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 プロパティをサポートしていないことに注意してください。

パラメータ

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