ALTER TABLE

適用於:標示為選取 Databricks SQL 標示為選取 Databricks Runtime

改變資料表的結構描述或屬性。

這個 ALTER TABLE 指令不支援臨時資料表。 若 ALTER TABLE 指令套用於暫存資料表,則會回傳錯誤。 如果資料表已快取,命令會清除該資料表和所有依賴它的項目的快取資料。 當表格或其從屬項再次被訪問時,緩存將被延遲填充。

在外表中,你只能執行 ALTER TABLE SET OWNERALTER TABLE RENAME TO

所需的權限

如果您使用 Unity Catalog ,則必須具有以下項目的 MODIFY 權限:

  • ALTER COLUMN
  • ADD COLUMN
  • DROP COLUMN
  • SET TBLPROPERTIES
  • UNSET TBLPROPERTIES
  • PREDICTIVE OPTIMIZATION

如果您使用 Unity Catalog,您必須擁有 MANAGE 許可權或擁有下列所有權:

  • SET OWNER TO

執行所有其他作業需要具有資料表的所有權。

語法

ALTER TABLE table_name
    { RENAME TO clause |
      ADD COLUMN clause |
      ALTER COLUMN clause |
      DROP COLUMN clause |
      RENAME COLUMN clause |
      DEFAULT COLLATION clause |
      ADD CONSTRAINT clause |
      DROP CONSTRAINT clause |
      DROP FEATURE clause |
      ADD PARTITION clause |
      DROP PARTITION clause |
      PARTITION SET LOCATION clause |
      RENAME PARTITION clause |
      RECOVER PARTITIONS clause |
      SET { ROW FILTER clause } |
      DROP ROW FILTER |
      SET TBLPROPERTIES clause |
      UNSET TBLPROPERTIES clause |
      SET SERDE clause |
      SET LOCATION clause |
      SET OWNER TO clause |
      SET SERDE clause |
      SET TAGS clause |
      UNSET TAGS clause |
      CLUSTER BY clause |
      PREDICTIVE OPTIMIZATION clause}

參數

  • table_name

    識別要改變的資料表。 名稱不得包含 時態規格或選項規格。 如果找不到數據表,Azure Databricks 就會引發 TABLE_OR_VIEW_NOT_FOUND 錯誤。

  • RENAME TO to_table_name

    重新命名數據表。

    • to_table_name

      識別新的資料表名稱。 名稱不得包含 時態規格或選項規格

      針對 Unity 目錄資料表, to_table_name 必須在與 table_name相同的目錄中。 對於其他數據表, to_table_name 必須在與 table_name相同的架構內。

      如果 to_table_name 未限定,則會隱含以當前結構限定。

    > ALTER TABLE student RENAME TO student_info;
    
  • 加 COLUMN

    將一個或多個資料行新增至資料表。

    當你在現有的 Delta Lake 資料表中新增欄位時,無法定義一個 DEFAULT 值。 所有新增到 Delta Lake 資料表的欄位都會視為 NULL 現有的列。 新增欄位後,你可以選擇性地用 ALTER COLUMN來定義新列的預設值。

    > DESCRIBE StudentInfo;
                    col_name data_type comment
    ----------------------- --------- -------
                        name    string    NULL
                      rollno       int    NULL
                         age       int    NULL
    
    > ALTER TABLE StudentInfo ADD columns (LastName string, DOB timestamp);
    
    -- After adding new columns to the table
    > DESCRIBE StudentInfo;
                    col_name data_type comment
    ----------------------- --------- -------
                        name    string    NULL
                      rollno       int    NULL
                    LastName    string    NULL
                         DOB timestamp    NULL
                         age       int    NULL
    
    -- Optionally set a default value for new rows
    > ALTER TABLE StudentInfo ALTER COLUMN LastName SET DEFAULT 'unknown';
    

  • 修改 COLUMN

    變更屬性或欄的位置。

    > DESCRIBE StudentInfo;
                    col_name data_type comment
    ----------------------- --------- -------
                        name    string    NULL
                      rollno       int    NULL
                    LastName    string    NULL
                         DOB timestamp    NULL
                         age       int    NULL
    
    > ALTER TABLE StudentInfo ALTER COLUMN name COMMENT "new comment";
    
    -- After altering the column
    > DESCRIBE StudentInfo;
                    col_name data_type     comment
    ----------------------- --------- -----------
                        name    string new comment
                      rollno       int        NULL
                    LastName    string        NULL
                         DOB timestamp        NULL
                         age       int        NULL
    

    在單一語句中修改多欄:

    -- Create a table with 3 columns
    > CREATE TABLE my_table (num INT, str STRING, bool BOOLEAN) TBLPROPERTIES('delta.feature.allowColumnDefaults' = 'supported')
    > DESCRIBE TABLE my_table;
      col_name    data_type     comment
      --------    ---------     -------
           num          int        null
           str       string        null
           bool      boolean       null
    
    -- Update comments on multiple columns
    > ALTER TABLE table ALTER COLUMN
       num COMMENT 'number column',
       str COMMENT 'string column';
    
    > DESCRIBE TABLE my_table;
      col_name    data_type      comment
      --------    ---------   -------------
           num          int   number column
           str       string   string column
          bool      boolean            null
    
    -- Can mix different types of column alter
    > ALTER TABLE table ALTER COLUMN
       bool COMMENT 'boolean column',
       num AFTER bool,
       str AFTER num,
       bool SET DEFAULT true;
    
    > DESCRIBE TABLE my_table;
      col_name    data_type      comment
      --------    ---------   --------------
          bool      boolean   boolean column
           num          int    number column
           str       string    string column
    
  • 刪除 COLUMN

    刪除 Delta Lake 資料表中的一或多個資料行或欄位。

  • 重新命名 COLUMN

    將 Delta Lake 資料表中的資料行或欄位重新命名。

    > ALTER TABLE StudentInfo RENAME COLUMN name TO FirstName;
    
    -- After renaming the column
    > DESCRIBE StudentInfo;
                    col_name data_type     comment
    ----------------------- --------- -----------
                   FirstName    string new comment
                      rollno       int        NULL
                    LastName    string        NULL
                         DOB timestamp        NULL
                         age       int        NULL
    

  • ADD CONSTRAINT

    將檢查約束、資訊性外鍵約束或資訊性主鍵約束新增至資料表。

    只有 Unity Catalog 中的資料表支援外部索引鍵和主索引鍵,而不是 hive_metastore 目錄。

  • DEFAULT COLLATION 排序名稱

    適用於:標示為是 Databricks SQL 標示為是 Databricks Runtime 16.3 和更新版本

    變更數據表中新 STRING 欄位的預設排序規則。 現有的欄位不會受到這個子句的影響。 若要變更現有資料列的定序,請使用 ALTER TABLE ... ALTER COLUMN ... COLLATE collation_name

  • DROP CONSTRAINT

    從資料表中卸除主鍵、外鍵或檢查約束。

  • DROP FEATURE feature_name [ TRUNCATE HISTORY ]

    適用於:check 標示為是 Databricks Runtime 14.3 LTS 和更新版本

    從 Databricks Runtime 14.3 LTS 開始提供舊版支援 DROP FEATURE 。 如需舊版功能的相關文件,請參閱卸除 Delta 資料表功能(舊版)。

    適用於:標示為是 Databricks SQL 標示為是 Databricks Runtime 16.3 和更新版本

  • Azure Databricks 建議針對所有 DROP FEATURE 命令使用 Databricks Runtime 16.3 和更新版本,以取代舊版行為。

    從 Delta Lake 資料表中移除功能。

    拿掉功能可能會導致在資料表通訊協定中新增 checkpointProtection 寫入器功能。 如需詳細資訊,請參閱 卸除 Delta 數據表功能和 數據表功能,以取得通訊協定相容性

    • feature_name

      功能名稱以STRING常值或識別符形式表示,必須能被 Azure Databricks 所識別並且在資料表上獲得支援。

      如果 Azure Databricks 數據表中沒有此功能,就會引發 DELTA_FEATURE_DROP_FEATURE_NOT_PRESENT

    • 刪除歷史

      透過截斷歷史紀錄來移除功能。 這需要兩個階段程式:

藉由截斷歷程記錄來移除功能需要兩個步驟的過程:

  • 第一次調用會清除功能的痕跡,並通知您部分的成功。

  • 然後,等待保留期結束,再次執行語句以完成移除。

    如果您過早起始第二個叫用,Azure Databricks 會引發 DELTA_FEATURE_DROP_WAIT_FOR_RETENTION_PERIODDELTA_FEATURE_DROP_HISTORICAL_VERSIONS_EXIST

    截斷數據表歷程記錄會限制您執行 DESCRIBE HISTORY 和執行時間移動查詢的能力。

    -- Drop the "deletion vectors" from a Delta table
    > ALTER TABLE my_table DROP FEATURE deletionVectors;
    
    -- 24 hours later
    > ALTER TABLE my_table DROP FEATURE deletionVectors TRUNCATE HISTORY;
    
  • 加 PARTITION

    將一個或多個分割區新增至資料表。

    > SHOW PARTITIONS StudentInfo;
    partition
    ---------
        age=11
        age=12
        age=15
    
    > ALTER TABLE StudentInfo ADD IF NOT EXISTS PARTITION (age=18);
    
    -- After adding a new partition to the table
    > SHOW PARTITIONS StudentInfo;
    partition
    ---------
        age=11
        age=12
        age=15
        age=18
    
    -- Adding multiple partitions to the table
    > ALTER TABLE StudentInfo ADD IF NOT EXISTS PARTITION (age=18) PARTITION (age=20);
    
    > SHOW PARTITIONS StudentInfo;
    partition
    ---------
        age=11
        age=12
        age=15
        age=18
        age=20
    
  • 刪除 PARTITION

    從資料表刪除一個或多個分割區。

    > SHOW PARTITIONS StudentInfo;
    partition
    ---------
        age=11
        age=12
        age=15
        age=18
    
    > ALTER TABLE StudentInfo DROP IF EXISTS PARTITION (age=18);
    
    -- After dropping the partition of the table
    > SHOW PARTITIONS StudentInfo;
    partition
    ---------
        age=11
        age=12
        age=15
    
  • PARTITION ... SET 位置

    設定分割區的位置。

    > ALTER TABLE dbx.tab1 PARTITION (a='1', b='2') SET LOCATION '/path/to/part/ways';
    
  • 重新命名 PARTITION

    更換分區的鍵。

    > SHOW PARTITIONS StudentInfo;
    partition
    ---------
        age=10
        age=11
        age=12
    
    > ALTER TABLE default.StudentInfo PARTITION (age='10') RENAME TO PARTITION (age='15');
    
    -- After renaming Partition
    > SHOW PARTITIONS StudentInfo;
    partition
    ---------
        age=11
        age=12
        age=15
    
  • 復原磁碟分區

    指示 Azure Databricks 掃描數據表的位置,並將任何檔案新增至已直接新增至文件系統的數據表。

  • SET ROW FILTER 條款

    適用於:標示為是 Databricks SQL 標示為是 Databricks Runtime 12.2 LTS 及以上版本 標示為僅限 Unity Catalog

    將資料列篩選函數新增至資料表。 資料表的所有後續查詢都會取得函式評估結果為布林值為真的資料列子集。 這適用於更細緻的存取控制,其中函式可以檢查叫用使用者的身分識別或群組成員資格,以決定是否要篩選某些資料列。

  • DROP ROW FILTER

    適用於:勾選為是] Unity 目錄

    如果有的話,從資料表移除行篩選條件。 未來的查詢將會從資料表傳回所有資料列,而不需要任何自動篩選。

  • SET TBL屬性

    設定或重設一個或多個使用者定義的屬性。

    > ALTER TABLE dbx.tab1 SET TBLPROPERTIES ('winner' = 'loser');
    
  • UNSET TBL屬性

    移除一個或多個使用者定義的屬性。

    > ALTER TABLE dbx.tab1 UNSET TBLPROPERTIES ('winner');
    
  • SET SERDE

    適用於:勾選「是」 Databricks Runtime

    指定用於在 Hive 格式資料表中讀寫資料的序列化器/解序列器(SerDe)類別。 你也可以用 WITH SERDEPROPERTIES來設定 SerDe 屬性。

    > ALTER TABLE test_tab SET SERDE 'org.apache.hadoop.hive.serde2.columnar.LazyBinaryColumnarSerDe';
    
    > ALTER TABLE dbx.tab1 SET SERDE 'org.apache.hadoop' WITH SERDEPROPERTIES ('k' = 'v', 'kay' = 'vee');
    
  • SET LOCATION

    移動資料表的位置。

    SET LOCATION path
    
    • LOCATION path

      path 必須是 STRING 文字表達式。 指定表格的新位置。

      原始位置中的檔案 將不會 移至新位置。

  • [ SET ] OWNER TO 校長

    將資料表的擁有權轉移至 principal

    適用於:勾選為是 Databricks SQL 勾選為是 Databricks Runtime 11.3 LTS 和更新版本

    允許 SET 作為可選關鍵字。

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

    適用於:標示為是 Databricks SQL 標示為是 Databricks Runtime 13.3 LTS 和更新版本

    將標籤套用至資料表。 您需要具有 APPLY TAG 權限,才能將標籤新增至資料表。

    • tag_name

      字面上的STRING. tag_name 在資料表或資料行內必須是唯一的。

    • tag_value

      字面上的STRING.

    -- Applies three tags to the table named `test`.
    > ALTER TABLE test SET TAGS ('tag1' = 'val1', 'tag2' = 'val2', 'tag3' = 'val3');
    
    -- Applies three tags to table `main.schema1.test` column `col1`.
    > ALTER TABLE main.schema1.test ALTER COLUMN col1 SET TAGS ('tag1' = 'val1', 'tag2' = 'val2', 'tag3' = 'val3');
    
  • UNSET TAGS ( tag_name [, ...] )

    適用於:標示為是 Databricks SQL 標示為是 Databricks Runtime 13.3 LTS 和更新版本

    從資料表中移除標籤。 您需要具有 APPLY TAG 權限,才能從資料表中移除標籤。

    • tag_name

      字面上的STRING. tag_name 在資料表或資料行內必須是唯一的。

    -- Removes three tags from the table named `test`.
    > ALTER TABLE test UNSET TAGS ('tag1', 'tag2', 'tag3');
    
    -- Removes three tags from table `main.schema1.test` column `col1`.
    > ALTER TABLE main.schema1.test ALTER COLUMN col1 UNSET TAGS ('tag1', 'tag2', 'tag3');
    
  • CLUSTER BY 條款

    適用於:標示為是 Databricks SQL 標示為是 Databricks Runtime 13.3 LTS 和更新版本

    新增、變更或卸除 Delta Lake 資料表的叢集策略。

  • { ENABLE | DISABLE | INHERIT } PREDICTIVE OPTIMIZATION

    適用於:標示為是 Databricks SQL 標示為是 Databricks Runtime 12.2 LTS 及以上版本 標示為僅限 Unity Catalog

    將受控 Delta Lake 資料表變更為所需的預測優化設定。

    根據預設,建立資料表時的預設行為是從數據架構中 INHERIT

    當預測優化被明確啟用或繼承為啟用時,OPTIMIZEVACUUM 將在 Azure Databricks 認為適當的資料表上自動叫用。 如需詳細資訊,請參閱: Unity 目錄受控數據表的預測優化

    -- Enables predictive optimization for my_table
    > ALTER TABLE my_table ENABLE PREDICTIVE OPTIMIZATION;
    

其他例子

如需查看 Delta Lake 新增條件約束和修改資料行的範例,請參閱