适用于:
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 Plus USE CATALOG 和 USE SCHEMA 的权限,目标的目录和模式。
对于 SET OWNER TO,所需的权限取决于新所有者:
- 如果主体是自己,则必须对视图拥有
MANAGE权限。 - 如果主体是组,则必须是该组的成员。
- 在所有其他情况下,你必须是元存储管理员。
语法
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 语句,但保留对视图授予的权限。
AS yaml_definition
适用于:
Databricks SQL,
Databricks Runtime 16.4 及以上版本,
用于指标视图的 YAML 定义。
此子句等效于现有视图上的 CREATE OR REPLACE VIEW 语句,但保留对视图授予的权限。
重新指定 YAML 定义会添加、删除或更改指标视图,
parameters而无需进行任何语法更改。 有关参数语法,请参阅 CREATE VIEW。 参数需要 Databricks Runtime 18.2 及更高版本。-
适用于:
Databricks SQL
Databricks Runtime 15.3 及更高版本指定视图的后续查询如何适应由于基础对象定义的更改而导致的视图架构的更改。 请参阅 CREATE VIEW……WITH SCHEMA,了解有关架构绑定模式的详细信息。
此子句不适用于指标视图。
[ SET ] OWNER TO 主体
将视图的所有权转移给
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 及更高版本取消设置标记 ( tag_name [, …] )
从视图中删除标记。 需要
APPLY TAG特权才能从视图中删除标记。适用于:
Databricks SQL
Databricks Runtime 13.3 LTS 及更高版本tag_name
文本
STRING。tag_name在视图中必须唯一。tag_value
文本
STRING。add_constraint
适用于:
Databricks SQL
Databricks 运行时
Unity 目录从度量视图到目标度量视图之间添加信息关系。 该条款处于 测试阶段。 两者都
view_nametarget_view必须是 Unity 目录的度量视图。 不支持临时度量视图。 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不同的别名。条件
定义在和
view_nametarget_view的列上的确定性BOOLEAN表达式,规则与ON连接的子句相同。 这个表达不能指代小节。 它不能调用聚合函数,包括 MEASURE 和 AGG、窗口函数或生成器。 最多可达1024个字符。正在使用(列[, ...])
在两个度量视图中,存在同名列的1到20列。
drop_constraint
适用于:
Databricks SQL
Databricks 运行时
Unity 目录这样可以从度量角度去掉关系
name。 该条款处于 测试阶段。 如果你指定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;