ALTER VIEW

適用於:檢查標示為是 Databricks SQL 檢查標示為是 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。 除非檢視是在 中 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

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

    可讓您在具體化檢視的排程中加入或改變排程。

    如果提供,請排程串流數據表或具體化檢視,以使用指定的 晶體 cron 排程重新整理其數據。 只 接受time_zone_values 。 不支援 AT TIME ZONE LOCAL。 如果 AT TIME ZONE 不存在,則會使用會話時區。 如果 AT TIME ZONE 不存在且未設定會話時區,則會擲回錯誤。 SCHEDULE 在語意上相當於 SCHEDULE REFRESH

    您無法在 SCHEDULE Delta Live Tables 管線定義中使用語法。

範例

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