共用方式為


ALTER TABLE

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

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

如需 Delta Lake 中的類型變更或重新命名數據行,請參閱 重寫數據

若要變更資料表或資料行的批註,您也可以使用 COMMENT ON

若要改變 STREAMING TABLE,請使用 ALTER STREAMING TABLE

如果資料表已快取,命令會清除該資料表和所有依賴它的項目的快取資料。 當表格或其從屬項再次被訪問時,緩存將被延遲填充。

備註

在現有 Delta 資料表中新增資料行時,無法定義 DEFAULT 值。 對於現有資料列,新增至 Delta 資料表的所有資料行都會被視為 NULL。 在新增資料行後,您可以選擇性地定義該資料行的預設值,但這只對插入資料表的新資料列生效。 使用下列語法:

ALTER TABLE table_name ALTER COLUMN column_name SET DEFAULT default_expression

在外部資料表中,您只能執行 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 未限定,則會隱含以當前結構限定。

  • 加 COLUMN

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

  • 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 和執行時間移動查詢的能力。

  • 加 PARTITION

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

  • 刪除 PARTITION

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

  • PARTITION ... SET 位置

    設定分割區的位置。

  • 重新命名 PARTITION

    更換分區的鍵。

  • 復原磁碟分區

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

  • SET ROW FILTER 條款

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

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

  • DROP ROW FILTER

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

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

  • SET TBLPROPERTIES

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

  • UNSET TBLPROPERTIES

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

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

  • UNSET TAGS ( tag_name [, ...] )

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

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

    • tag_name

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

  • 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 目錄受控數據表的預測優化

範例

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

-- RENAME table
> DESCRIBE student;
                col_name data_type comment
 ----------------------- --------- -------
                    name    string    NULL
                  rollno       int    NULL
                     age       int    NULL
 # Partition Information
              # col_name data_type comment
                     age       int    NULL

> ALTER TABLE Student RENAME TO StudentInfo;

-- After Renaming the table
> DESCRIBE StudentInfo;
                col_name data_type comment
 ----------------------- --------- -------
                    name    string    NULL
                  rollno       int    NULL
                     age       int    NULL
 # Partition Information
              # col_name data_type comment
                     age       int    NULL

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

-- Add new columns to a table
> DESCRIBE StudentInfo;
                col_name data_type comment
 ----------------------- --------- -------
                    name    string    NULL
                  rollno       int    NULL
                     age       int    NULL
 # Partition Information
              # col_name data_type comment
                     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
 # Partition Information
              # col_name data_type comment
                     age       int    NULL

-- Add a new partition to a table
> 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

-- Drop a partition from the table
> 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

-- Adding multiple partitions to the table
> SHOW PARTITIONS StudentInfo;
 partition
 ---------
    age=11
    age=12
    age=15

> ALTER TABLE StudentInfo ADD IF NOT EXISTS PARTITION (age=18) PARTITION (age=20);

-- After adding multiple partitions to the table
> SHOW PARTITIONS StudentInfo;
 partition
 ---------
    age=11
    age=12
    age=15
    age=18
    age=20

-- ALTER or CHANGE COLUMNS
> DESCRIBE StudentInfo;
                col_name data_type comment
+-----------------------+---------+-------
                    name    string    NULL
                  rollno       int    NULL
                LastName    string    NULL
                     DOB timestamp    NULL
                     age       int    NULL
 # Partition Information
              # col_name data_type comment
                     age       int    NULL

ALTER TABLE StudentInfo ALTER COLUMN name COMMENT "new comment";

--After ALTER or CHANGE COLUMNS
> DESCRIBE StudentInfo;
                col_name data_type     comment
 ----------------------- --------- -----------
                    name    string new comment
                  rollno       int        NULL
                LastName    string        NULL
                     DOB timestamp        NULL
                     age       int        NULL
 # Partition Information
              # col_name data_type     comment
                     age       int        NULL

-- RENAME COLUMN
> ALTER TABLE StudentInfo RENAME COLUMN name TO FirstName;

--After RENAME COLUMN
> DESCRIBE StudentInfo;
                col_name data_type     comment
 ----------------------- --------- -----------
               FirstName    string new comment
                  rollno       int        NULL
                LastName    string        NULL
                     DOB timestamp        NULL
                     age       int        NULL
 # Partition Information
              # col_name data_type     comment
                     age       int        NULL

-- Change the file Location
> ALTER TABLE dbx.tab1 PARTITION (a='1', b='2') SET LOCATION '/path/to/part/ways';

-- SET SERDE/ SERDE Properties (DBR only)
> 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 TABLE PROPERTIES
> ALTER TABLE dbx.tab1 SET TBLPROPERTIES ('winner' = 'loser');

-- DROP TABLE PROPERTIES
> ALTER TABLE dbx.tab1 UNSET TBLPROPERTIES ('winner');

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

-- Applies three tags to the table named `test`.
> ALTER TABLE test SET TAGS ('tag1' = 'val1', 'tag2' = 'val2', 'tag3' = 'val3');

-- Removes three tags from the table named `test`.
> ALTER TABLE test UNSET TAGS ('tag1', 'tag2', 'tag3');

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

-- Removes three tags from table `main.schema1.test` column `col1`.
> ALTER TABLE main.schema1.test ALTER COLUMN col1 UNSET TAGS ('tag1', 'tag2', 'tag3');

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

-- Alter multiple columns in a single statement
-- 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