ALTER VIEW

适用于:check marked yes Databricks SQL check marked yes Databricks Runtime

更改与视图关联的元数据。 它可以更改视图的定义、对视图重命名,还可通过设置 TBLPROPERTIES 来设置和取消设置视图的元数据。

如果已缓存视图,则该命令将清除该视图及其引用的所有依赖项的缓存数据。 下一次访问视图时,视图的缓存将被延迟填充。 此命令将视图的依赖项保留为未缓存。

语法

ALTER [ MATERIALIZED ] VIEW view_name
  { rename |
    SET TBLPROPERTIES clause |
    UNSET TBLPROPERTIES clause |
    alter_body |
    owner_to |
    schedule
    SET TAGS clause |
    UNSET TAGS clause }}}

rename
  RENAME TO to_view_name

alter_body
  AS query

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

owner_to
  [ SET ] OWNER TO principal

schedule
  {
    { ADD | ALTER } SCHEDULE [ REFRESH ]
      CRON cron_string [ AT TIME ZONE timezone_id ] |
    DROP SCHEDULE
  }

参数

  • view_name

    标识要更改的视图。 如果找不到视图,Azure Databricks 会引发 TABLE_OR_VIEW_NOT_FOUND 错误。

  • 重命名为 to_view_name

    重命名架构中的现有视图。 无法重命名具体化视图。

    to_view_name 指定视图的新名称。 如果 to_view_name 已存在,则引发 TableAlreadyExistsException。 如果 to_view_name 是限定名称,则必须与 view_name架构名称匹配。

  • SET TBLPROPERTIES

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

  • UNSET TBLPROPERTIES

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

  • AS 查询

    从基表或其他视图中构造视图的查询。

    此子句等效于现有视图上的 CREATE OR REPLACE VIEW 语句,但保留对视图授予的权限。

  • [ SET ] OWNER TO principal

    将视图的所有权转移给 principal。 除非在 hive_metastore 中定义了视图,否则只能将所有权转移给你所属的组。

    适用于:check marked yes Databricks SQL SQL 仓库版本 2022.35 或更高版本 check marked yes Databricks Runtime 11.2 及更高版本

    允许使用 SET 作为可选关键字。

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

    将标记应用于视图。 你需要具有 apply_tag 权限才能向视图添加标记。

    适用于:check marked yes Databricks SQL check marked yes Databricks Runtime 13.3 LTS 及更高版本

  • UNSET TAGS ( tag_name [, …] )

    从表中删除标记。 你需要具有 apply_tag 权限才能从视图中删除标记。

    适用于:check marked yes Databricks SQL check marked yes Databricks Runtime 13.3 LTS 及更高版本

  • tag_name

    文本 STRINGtag_name 在视图中必须唯一。

  • tag_value

    文本 STRING

  • SCHEDULE [ REFRESH ] CRON cron_string [ AT TIME ZONE timezone_id ]

    允许向具体化视图添加计划或更改其计划。

    如果提供计划,则它会流式处理表或具体化视图,以使用给定的 quartz cron 计划刷新其数据。 仅接受 time_zone_values。 不支持 AT TIME ZONE LOCAL。 如果 AT TIME ZONE 不存在,则使用会话时区。 如果 AT TIME ZONE 不存在并且未设置会话时区,则会引发错误。 SCHEDULE 在语义上等效于 SCHEDULE REFRESH

    不能在增量实时表管道定义中使用 SCHEDULE 语法。

示例

-- 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`

-- Adds a schedule to refresh a materialized view once a day
-- at midnight in Los Angeles
> ALTER MATERIALIZED VIEW my_mv
    ADD SCHEDULE CRON '0 0 0 * * ? *' AT TIME ZONE 'America/Los_Angeles';

-- Alters the schedule to run every 15 minutes for a materialized view
> ALTER MATERIALIZED VIEW my_mv
    ALTER SCHEDULE CRON '0 0/15 * * * ? *';

-- Drops the schedule for a materialized view
> ALTER MATERIALIZED VIEW my_mv
    DROP SCHEDULE;

-- 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');