共用方式為


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
  • modify PREDICTIVE OPTIMIZATION

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

語法

ALTER TABLE table_name
   { RENAME TO clause |
     ADD COLUMN clause |
     ALTER COLUMN clause |
     DROP COLUMN clause |
     RENAME COLUMN 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 TOto_table_name

    重新命名相同結構描述內的資料表。

  • ADD COLUMN

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

  • ALTER COLUMN

    變更屬性或資料行的位置。

  • DROP COLUMN

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

  • RENAME COLUMN

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

  • ADD CONSTRAINT

    將 CHECK 條件約束、資訊外部索引鍵條件約束或資訊主索引鍵條件約束新增至資料表。

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

  • DROP CONSTRAINT

    從資料表卸除主索引鍵、外部索引鍵或 CHECK 條件約束。

  • DROP FEATURE feature_name [ TRUNCATE HISTORY ]

    適用於: 核取記號為「是」 Databricks SQL 核取記號為「是」 Databricks Runtime 14.1 和更新版本

    從 Delta Lake 資料表移除功能。

    移除影響讀取器和寫入器的功能需要一個兩階段處理過程:

    如需詳細資訊,請參閱什麼是資料表功能?

    • feature_name

      STRING 常值或識別碼形式表示的功能名稱,必須被 Azure Databricks 理解,並且在資料表上受到支援。

      支援的 feature_names 為:

      • ‘deletionVectors’ 或 deletionvectors
        • ‘v2Checkpoint’ 或 v2checkpoint

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

    • TRUNCATE HISTORY

      您可以選擇在 24 小時後啟動卸除讀取器加寫入器功能的第二個階段,方法是在執行調用命令時截斷資料表歷史記錄。

      截斷資料表歷史記錄會限制您執行 DESCRIBE HISTORY 和執行時間旅行查詢。

  • ADD PARTITION

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

  • DROP PARTITION

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

  • PARTITION … SET LOCATION

    設定分割區的位置。

  • RENAME PARTITION

    取代分割區的索引鍵。

  • RECOVER PARTITIONS

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

  • SETROW FILTER 子句

    適用於:核取記號為「是」Databricks SQL 核取記號為「是」 Databricks Runtime 12.2 LTS 和更新版本 核取記號為「是」 僅限 Unity Catalog

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

  • DROP ROW FILTER

    適用於:核取記號為「是」僅限 Unity Catalog

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

  • SET TBLPROPERTIES

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

  • UNSET TBLPROPERTIES

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

  • SET LOCATION

    移動資料表的位置。

    SET LOCATION path
    
    • LOCATION path

      path 必須是 STRING 常值。 指定資料表的新位置。

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

  • [ SET ] OWNER TOprincipal

    將資料表的擁有權轉移至 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

      常值 STRINGtag_name 在資料表或資料行內必須是唯一的。

    • tag_value

      常值 STRING

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

    適用於:核取記號為「是」 Databricks SQL 核取記號為「是」 Databricks Runtime 13.3 LTS 和更新版本

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

    • tag_name

      常值 STRINGtag_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 Catalog 受控資料表的預測性最佳化

範例

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