適用於:
Databricks SQL
Databricks Runtime
變更與視圖相關的元資料。 它可以更改視圖的定義、重新命名視圖,並透過設定 TBLPROPERTIES來設定或取消元資料。 對於度量視圖,它也能新增或刪除 關係 (Beta)。
若要在視圖上新增或變更註解,請使用 COMMENT ON。
如果快取檢視,命令會清除檢視的快取數據,以及參考該檢視的所有相依專案。 下一次存取視圖時,視圖的快取會被懶散地填滿。 指令會將視圖的依賴項保留為未快取狀態。
所需權限
如果您使用 Unity 目錄,以下操作不需要擁有該檢視圖的所有權:
-
SET TAGS: 需要特APPLY TAG權。 -
UNSET TAGS: 需要特APPLY TAG權。
以下操作需要擁有該視圖的所有權:
SET TBLPROPERTIESUNSET TBLPROPERTIES-
AS query(變更檢視定義) -
WITH SCHEMA(變更結構綁定)
RENAME TO 需要對視角擁有 MANAGE 特權。
ADD CONSTRAINT並DROP CONSTRAINT要求擁有MANAGE度量視圖或所有權的特權,以及USE CATALOGUSE SCHEMA其目錄與結構。
ADD CONSTRAINT 對於關係,也需要對目標指標檢視、 SELECT 加上 USE CATALOG 和 USE 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
參數
-
識別要改變的檢視。 如果找不到該檢視,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未限定,則在當前模式中隱含限定。具體化的檢視無法重新命名。
-
設定或重設一個或多個使用者定義的屬性。
-
移除一個或多個使用者定義的屬性。
AS 查詢
從基表或其他視圖建構檢視的查詢。
AS query不支援計量檢視功能。這個子句相當於 現有檢視上的 CREATE OR REPLACE VIEW 語句,但檢視上授與的許可權會保留。
作為yaml_definition
適用於:
Databricks SQL
Databricks Runtime 16.4 和更高版本
Unity Catalog 仅适用計量檢視的yaml_definition。
這個子句相當於 現有檢視上的 CREATE OR REPLACE VIEW 語句,但檢視上授與的許可權會保留。
重新指定 YAML 定義會新增、刪除或更改度量視圖,
parameters語法不會改變。 關於參數語法,請參見 CREATE VIEW。 參數需要 Databricks Runtime 18.2 及以上版本。-
適用於:
Databricks SQL
Databricks Runtime 15.3 和更新版本指定後續查詢如何適應由於基礎物件定義變更而對檢視架構所做的更改。 請參閱 CREATE VIEW...WITH SCHEMA 如需架構系結模式的詳細數據。
此子句不適用於計量檢視。
[ SET ] 擁有者 至 主要
將檢視的擁有權轉移至
principal。 除非視圖在 中定義hive_metastore,否則你只能將所有權轉移給你所屬的群組。適用於:
Databricks SQL
Databricks Runtime 11.3 LTS 和更新版本允許
SET作為可選關鍵字。SET TAGS ( { tag_name = tag_value } [, ...] )
對視圖套用標籤。 你需要有
APPLY TAG權限在檢視中新增標籤。適用於:
Databricks SQL
Databricks Runtime 13.3 LTS 和更新版本UNSET TAGS (tag_name [, ...] )
移除檢視中的標籤。 你需要有
APPLY TAG權限從檢視中移除標籤。適用於:
Databricks SQL
Databricks Runtime 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_alias
可選擇性地將目標命名為
condition。 從度量觀點與自身的關係需要一個與 名稱view_name不同的別名。條件
一個定義在 和
target_view的view_name列上的確定性BOOLEAN表達式,其規則與ONjoin 的子句相同。 這個表達式無法指涉小節。 它無法呼叫包含 MEASURE 和 AGG 在內的聚合函式、視窗函式或產生器。 最多可達 1,024 個字元。正在使用(欄位 [, ...])
在兩個度量視圖中,存在同名的欄位只有1到20個。
drop_constraint
適用於:
Databricks SQL
Databricks 執行時間 19 及以上
僅限 Unity 目錄這樣就能從度量角度去掉關係
name。 這條款目前還在 Beta 階段。 如果你指定IF EXISTS,當該名稱不存在關係時,該陳述即成功。 否則,該方法將失敗。NO_SUCH_CONSTRAINT_EXCEPTIONWarning
不要特別說明
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;