ALTER VIEW

適用於:已勾選為 Databricks SQL 勾選為 Databricks 執行時

變更與視圖相關的元資料。 它可以更改視圖的定義、重新命名視圖,並透過設定 TBLPROPERTIES來設定或取消元資料。 對於度量視圖,它也能新增或刪除 關係 (Beta)。

若要在視圖上新增或變更註解,請使用 COMMENT ON。

如果快取檢視,命令會清除檢視的快取數據,以及參考該檢視的所有相依專案。 下一次存取視圖時,視圖的快取會被懶散地填滿。 指令會將視圖的依賴項保留為未快取狀態。

所需權限

如果您使用 Unity 目錄,以下操作不需要擁有該檢視圖的所有權:

  • SET TAGS: 需要特 APPLY TAG 權。
  • UNSET TAGS: 需要特 APPLY TAG 權。

以下操作需要擁有該視圖的所有權:

  • SET TBLPROPERTIES
  • UNSET TBLPROPERTIES
  • AS query (變更視角定義)
  • WITH SCHEMA (變更結構綁定)

RENAME TO 需要對該視圖的 MANAGE 權限。

ADD CONSTRAINT並DROP CONSTRAINT需要對MANAGE其指標檢視或擁有權,以及USE CATALOGUSE SCHEMA其目錄與結構的權限。 ADD CONSTRAINT對於關聯,也需要對目標指標檢視、SELECT加上USE CATALOGUSE SCHEMA以及目標目錄與結構的權限。

對於 SET OWNER TO,所需的權限取決於新擁有者:

  • 如果校長是你自己,你必須擁有 MANAGE 觀賞權。
  • 如果校長是團體,你必須是該團體的成員。
  • 其他情況下,你必須是 metastore 管理員。

語法

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

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
  { identifier [. ...] | string_literal }

owner_to
  [ SET ] OWNER TO principal

add_constraint
  ADD [ CONSTRAINT name ] relationship_constraint

relationship_constraint
  cardinality RELATIONSHIP TO target_view [ [ AS ] target_alias ]
    { ON condition | USING ( column [, ...] ) }

cardinality
  { MANY | ONE } TO { MANY | ONE }

drop_constraint
  DROP CONSTRAINT [ IF EXISTS ] name

參數

  • view_name

    識別要改變的檢視。 如果找不到該檢視,Azure Databricks 會發出 TABLE_OR_VIEW_NOT_FOUND 錯誤。

  • 重新命名為 to_view_name

    將現有的檢視重新命名為 to_view_name。

    針對 Unity 目錄檢視, to_view_name 必須在與 view_name相同的目錄中。 對於其他檢視, to_view_name 必須在與 view_name相同的架構內。

    若 to_view_name 未限定,則在當前模式中隱含限定。

    具體化的檢視無法重新命名。

  • SET TBL屬性

    設定或重設一個或多個使用者定義的屬性。

  • UNSET TBLPROPERTIES

    移除一個或多個使用者定義的屬性。

  • AS 查詢

    從基表或其他視圖建構檢視的查詢。

    AS query 不支援度量檢視。

    這個子句相當於 現有檢視上的 CREATE OR REPLACE VIEW 語句,但檢視上授與的許可權會保留。

  • 如yaml_definition

    適用於:勾選是 Databricks SQL 勾選是 Databricks 執行環境 16.4 及以上版本 勾 選是 僅 Unity 目錄

    計量檢視的yaml_definition。

    這個子句相當於 現有檢視上的 CREATE OR REPLACE VIEW 語句,但檢視上授與的許可權會保留。

    重新指定 YAML 定義會新增、刪除或更改度量視圖, parameters 語法不會改變。 關於參數語法,請參見 CREATE VIEW。 參數需要 Databricks Runtime 18.2 及以上版本。

  • schema_binding

    適用於:勾選為是 、Databricks SQL 勾選為是 、Databricks 執行環境 15.3 及以上版本

    指定後續查詢如何適應由於基礎物件定義變更而對檢視架構所做的更改。 請參閱 CREATE VIEW...WITH SCHEMA 如需架構系結模式的詳細數據。

    此子句不適用於計量檢視。

  • [ SET ] 業主致 校長

    將檢視的擁有權轉移至 principal。 除非視圖在 中定義 hive_metastore,否則你只能將所有權轉移給你所屬的群組。 只有 Metastore 管理員才能將所有權轉移給任何其他主體。 請參閱 「必要權限」。

    適用範圍:勾選為「是」 Databricks SQL 勾選為「是」 Databricks Runtime 11.3 LTS 和其以上版本

    SET 允許作為選填關鍵字。

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

    對視圖套用標籤。 你需要有 APPLY TAG 權限在檢視中新增標籤。

    適用於:勾選標記為是 Databricks SQL 勾選為是 Databricks 執行時間 13.3 LTS 及以上

  • 未設定標籤(tag_name [, ...])

    移除檢視中的標籤。 你需要有 APPLY TAG 權限從檢視中移除標籤。

    適用於:勾選標記為是 Databricks SQL 勾選為是 Databricks 執行時間 13.3 LTS 及以上

  • tag_name

    常值 STRING。 在 tag_name 檢視中必須是唯一的。

  • tag_value

    常值 STRING。

  • add_constraint

    適用於:勾選為是 Databricks SQL 勾選為是 Databricks 執行環境 19 及以上 勾選是 僅 Unity 目錄

    從度量視角到目標度量視圖,加入資訊關係。 這條款目前還在 Beta 階段。 兩者都view_nametarget_view必須是 Unity Catalog 的度量檢視。 暫時的度量視圖不支援。 Azure Databricks 不會強制執行關聯。 參見 度量視圖之間的關係。

    • CONSTRAINT 名稱

      可選擇性地命名關係。 名稱必須在結構中所有約束中唯一。 如果你省略了它,Azure Databricks 會用兩個指標檢視的未限定名稱來命名關係 <source>_to_<target>。

    • 基數

      其中MANY TO ONE、ONE TO MANY、ONE TO ONE或MANY TO MANY。 Azure Databricks 不會用基數來檢查資料。

    • target_view

      識別目標指標視圖。 目標可以是同一元儲存庫的任何目錄或結構中。

    • 就像target_alias

      可選擇性地將目標命名為 condition。 從度量觀點與自身的關係需要一個與 名稱 view_name不同的別名。

    • 條件允許

      一個定義在 和 target_view的view_name列上的確定性BOOLEAN表達式,其規則與 ON join 的子句相同。 這個表達式無法指涉小節。 它無法呼叫包含 MEASURE 和 AGG 在內的聚合函式、視窗函式或產生器。 最多可達 1,024 個字元。

    • 使用(欄位[, ...])

      在兩個度量視圖中,存在同名的欄位只有1到20個。

  • drop_constraint

    適用於:勾選為是 Databricks SQL 勾選為是 Databricks 執行環境 19 及以上 勾選是 僅 Unity 目錄

    這樣就能從度量角度去掉關係 name 。 這條款目前還在 Beta 階段。 如果你指定 IF EXISTS,當該名稱不存在關係時,該陳述即成功。 否則,該方法將失敗。NO_SUCH_CONSTRAINT_EXCEPTION

    Warning

    不要特別說明RESTRICTCASCADE你什麼時候結束一段關係。 與 ALTER TABLE DROP CONSTRAINT不同,這些關鍵字沒有明確的關係行為,且其行為可能會在未來版本中改變。

範例

-- Rename only changes the view name.
-- The source and target schemas of the view must 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;

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

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

-- Alter 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
 order_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: 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)
   "
 Language                    YAML
 Table Properties            [metric_view.from.name=samples.tpch.orders, metric_view.from.type=ASSET, metric_view.where=o_orderdate > '1990-01-01']

-- Declare that each order in the `orders_mv` metric view matches one customer
-- in the `customers_mv` metric view, joined on the shared `customer_id` column.
> ALTER VIEW main.sales.orders_mv ADD CONSTRAINT orders_customer
    MANY TO ONE RELATIONSHIP TO main.sales.customers_mv
    USING (customer_id);

-- Declare a relationship that refers to the target by an alias in the ON condition.
> ALTER VIEW main.travel.flights_mv ADD CONSTRAINT departure_city
    MANY TO ONE RELATIONSHIP TO main.travel.cities_mv AS c
    ON flights_mv.departure_city_id = c.city_id;

-- Drop the relationship.
> ALTER VIEW main.travel.flights_mv DROP CONSTRAINT IF EXISTS departure_city;