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_name 位于同 view_name一目录中。 对于其他视图,to_view_name 必须位于与 view_name 相同的架构中。

    如果 to_view_name 不符合条件,则使用当前架构隐式限定。

    无法重命名具体化视图。

    设置或重置一个或多个用户定义的属性。

  • UNSET TBLPROPERTIES

    移除一个或多个用户定义的属性。

  • 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……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

    文本 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']