次の方法で共有


ALTER VIEW

適用対象:「はい」のチェック マーク Databricks SQL 「はい」のチェック マーク Databricks Runtime

ビューに関連付けられたメタデータを変更します。 ビューの定義を変更する、ビューの名前を別の名前に変更する、または TBLPROPERTIES を設定してビューのメタデータを設定および設定解除できます。

ビューまたはその列にコメントを追加または変更するには、COMMENT ONを使用します。

ビューがキャッシュされている場合、このコマンドを使用して、ビューのキャッシュされたデータとそれを参照するすべての依存をクリアします。 次回ビューにアクセスすると、ビューのキャッシュが遅延して格納されます。 このコマンドは、ビューの依存ファイルをキャッシュされていない状態のままにします。

構文

ALTER VIEW view_name
  { rename |
    SET TBLPROPERTIES clause |
    UNSET TBLPROPERTIES clause |
    alter_body |
    schema_binding |
    owner_to |
    SET TAGS clause |
    UNSET TAGS clause }

rename
  RENAME TO to_view_name

alter_body
AS { query | yaml_definition }

yaml_definition
  $$
    yaml_string
  $$

schema_binding
  WITH SCHEMA { BINDING | [ TYPE ] EVOLUTION | COMPENSATION }

property_key
  { idenitifier [. ...] | string_literal }

owner_to
  [ SET ] OWNER TO principal

パラメーター

  • view_name

    変更するビューを指定します。 ビューが見つからない場合、Azure Databricks では TABLE_OR_VIEW_NOT_FOUND エラーが生じます。

  • 名前をto_view_nameに変更する

    既存のビューの名前を to_view_nameに変更します。

    Unity カタログ ビューの場合、 to_view_nameview_nameと同じカタログ内にある必要があります。 他のビューの場合、 to_view_nameview_nameと同じスキーマ内にある必要があります。

    to_view_nameが修飾されていない場合、現在のスキーマで暗黙的に修飾されます。

    具体化されたビューの名前を変更することはできません。

    1 つ以上のユーザー定義プロパティを設定またはリセットします。

  • UNSET TBLPROPERTIES(テーブルプロパティの解除)

    1 つ以上のユーザー定義プロパティを削除します。

  • AS クエリ

    ベース テーブルまたはその他のビューからビューを構築するクエリです。

    AS query は、メトリック ビューではサポートされていません。

    この句は、ビューに付与された権限が保持される点を除いて、既存のビューの CREATE OR REPLACE VIEW ステートメントと同じです。

  • AS yaml_definition

    適用対象:はい Databricks SQL はい Databricks Runtime 16.4以降 はい Unity Catalogのみ

    メトリックビューのYAML定義。

    この句は、ビューに付与された権限が保持される点を除いて、既存のビューの CREATE OR REPLACE VIEW ステートメントと同じです。

  • スキーマ結合(schema_binding)

    適用対象:チェックマークが付いた「はい」 Databricks SQL チェックマークが付いた「はい」 Databricks Runtime 15.3以降

    基になるオブジェクト定義の変更により、ビューの後続のクエリがビューのスキーマの変更にどのように適応するかを指定します。 スキーマ バインド モードの詳細については、CREATE VIEWを参照してください。SCHEMA も一緒にご覧ください。

    この句は、メトリック ビューではサポートされていません。

  • [ SET ] 所有者を主要

    ビューの所有権を principal に移します。 hive_metastore でビューが定義されていない限り、自分が属しているグループにのみ所有権を移すことができます。

    適用対象:check marked yes Databricks SQL 「はい」のチェックマーク Databricks Runtime 11.3 LTS 以上

    SET は省略可能なキーワードとして使用できます。

  • SET TAGS ( { tag_name = tag_value } [, ...] )

    ビューにタグを適用します。 ビューにタグを追加するには、APPLY TAG アクセス許可が必要です。

    適用対象:check marked yes Databricks SQL Databricks Runtime 13.3 LTS 以上

  • タグを解除 (タグ名 [, …])

    テーブルからタグを削除します。 ビューからタグを削除するには、APPLY TAG アクセス許可が必要です。

    適用対象:check marked yes Databricks SQL Databricks Runtime 13.3 LTS 以上

  • tag_name

    リテラル STRINGtag_name はビュー内で一意である必要があります。

  • tag_value

    リテラル STRING

-- Rename only changes the view name.
-- The source and target schemas of the view have to be the same.
-- Use qualified or unqualified name for the source and target view.
> ALTER VIEW tempsc1.v1 RENAME TO tempsc1.v2;

-- Verify that the new view is created.
> DESCRIBE TABLE EXTENDED tempsc1.v2;
                            c1       int   NULL
                            c2    string   NULL

  # Detailed Table Information
                      Database   tempsc1
                         Table        v2

-- Before ALTER VIEW SET TBLPROPERTIES
> DESCRIBE TABLE EXTENDED tempsc1.v2;
                            c1       int   null
                            c2    string   null

  # Detailed Table Information
                      Database   tempsc1
                         Table        v2
              Table Properties    [....]

-- Set properties in TBLPROPERTIES
> ALTER VIEW tempsc1.v2 SET TBLPROPERTIES ('created.by.user' = "John", 'created.date' = '01-01-2001' );

-- Use `DESCRIBE TABLE EXTENDED tempsc1.v2` to verify
> DESCRIBE TABLE EXTENDED tempsc1.v2;
                            c1                                                   int   NULL
                            c2                                                string   NULL

  # Detailed Table Information
                      Database                                               tempsc1
                         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 tempsc1.v2 UNSET TBLPROPERTIES (`created`.`by`.`user`, created.date);

-- Use `DESCRIBE TABLE EXTENDED tempsc1.v2` to verify the changes
> DESCRIBE TABLE EXTENDED tempsc1.v2;
                            c1       int   NULL
                            c2    string   NULL

  # Detailed Table Information
                      Database   tempsc1
                         Table        v2
              Table Properties    [....]

-- Change the view definition
> ALTER VIEW tempsc1.v2 AS SELECT * FROM tempsc1.v1;

-- Use `DESCRIBE TABLE EXTENDED` to verify
> DESCRIBE TABLE EXTENDED tempsc1.v2;
                            c1                        int   NULL
                            c2                     string   NULL

  # Detailed Table Information
                      Database                    tempsc1
                         Table                         v2
                          Type                       VIEW
                     View Text   select * from tempsc1.v1
            View Original Text   select * from tempsc1.v1

-- Transfer ownership of a view to another user
> ALTER VIEW v1 OWNER TO `alf@melmak.et`

-- Change the view schema binding to adopt type evolution
> ALTER VIEW v1 WITH SCHEMA TYPE EVOLUTION;

-- Applies three tags to the view named `test`.
> ALTER VIEW test SET TAGS ('tag1' = 'val1', 'tag2' = 'val2', 'tag3' = 'val3');

-- Removes three tags from the view named `test`.
> ALTER VIEW test UNSET TAGS ('tag1', 'tag2', 'tag3');

-- Alter a the metric view `region_sales_metrics` defined in CREATE VIEW to drop the `total_revenue_for_open_orders` measure.
> ALTER VIEW region_sales_metrics
  AS $$
   version: 0.1
   source: samples.tpch.orders
   filter: o_orderdate > '1990-01-01'
   dimensions:
   - name: month
     expr: date_trunc('MONTH', o_orderdate)
   - name: status
     expr: case
       when o_orderstatus = 'O' then 'Open'
       when o_orderstatus = 'P' then 'Processing'
       when o_orderstatus = 'F' then 'Fulfilled'
       end
   - name: order_priority
     expr: split(o_orderpriority, '-')[1]
   measures:
   - name: count_orders
     expr: count(1)
   - name: total_revenue
     expr: SUM(o_totalprice)
   - name: total_revenue_per_customer
     expr: SUM(o_totalprice) / count(distinct o_custkey)
  $$;

> DESCRIBE EXTENDED region_sales_metrics;
 col_name                    data_type
 month	                     timestamp
 status	                     string
 prder_priority              string
 count_orders                bigint measure
 total_revenue               decimal(28,2) measure
 total_revenue_per_customer  decimal(38,12) measure

 # Detailed Table Information
 Catalog                     main
 Database                    default
 Table                       region_sales_metrics
 Owner                       alf@melmak.et
 Created Time                Sun May 18 23:45:25 UTC 2025
 Last Access                 UNKNOWN
 Created By                  Spark
 Type                        METRIC_VIEW
 Comment                     A metric view for regional sales metrics.
 View Text                   "
    version: 0.1
    source: samples.tpch.orders
    filter: o_orderdate > '1990-01-01'
    dimensions:
    - name: month
      expr: date_trunc('MONTH', o_orderdate)
    - name: status
      expr: case
        when o_orderstatus = 'O' then 'Open'
        when o_orderstatus = 'P' then 'Processing'
        when o_orderstatus = 'F' then 'Fulfilled'
        end
    - name: prder_priority
      expr: split(o_orderpriority, '-')[1]
    measures:
    - name: count_orders
      expr: count(1)
    - name: total_revenue
      expr: SUM(o_totalprice)
    - name: total_revenue_per_customer
      expr: SUM(o_totalprice) / count(distinct o_custkey)
   "
 Language                    YAML
 Table Properties            [metric_view.from.name=samples.tpch.orders, metric_view.from.type=ASSET, metric_view.where=o_orderdate > '1990-01-01']