ALTER TABLE

适用于:勾选“是” Databricks SQL 勾选“是” Databricks Runtime

更改表的架构或属性。

临时表支持 ALTER TABLE ... SET TBLPROPERTIESALTER TABLE ... UNSET TBLPROPERTIES 无服务器计算、Databricks Runtime 18.2 及更高版本以及 Databricks SQL 2026.15 及更高版本。 临时表不支持其他 ALTER TABLE 子句并返回错误。 如果已缓存表,则该命令将清除该表及其引用的所有依赖项的缓存数据。 在下次访问该表或这些依赖项时,将会延迟填充缓存。

在外表上,只能执行 ALTER TABLE SET OWNERALTER TABLE RENAME TO

所需的权限

如果使用 Unity Catalog,则必须具有 MODIFY 权限:

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

如果您使用 Unity Catalog,则必须对以下内容拥有 MANAGE 权限或所有权:

  • SET OWNER TO
  • PREDICTIVE OPTIMIZATION

所有其他操作都需要表的所有权。

语法

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 TAGS clause |
      UNSET TAGS clause |
      CLUSTER BY clause |
      REPLACE PARTITIONED BY WITH 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 现有行。 添加列后,可以选择使用 为新行定义默认值。

    > 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

    向表添加 CHECK 约束、信息性外键约束或信息性主键约束。

    仅 Unity Catalog 中的表支持外键和主键,而 hive_metastore 目录不支持。

  • DEFAULT COLLATION collation_name

    适用于:已选中“是” Databricks SQL 已选中“是” Databricks Runtime 16.3 及更高版本

    更改新 STRING 列的表的默认排序规则。 现有列不受此子句的影响。 若要更改现有列的排序规则,请使用 ALTER TABLE ... ALTER COLUMN ... COLLATE collation_name

  • DROP CONSTRAINT

    从表中删除主键、外键或 CHECK 约束。

  • DROP FEATURE feature_name [ TRUNCATE HISTORY ]

    适用于:检查标记为是 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 编写器功能。 有关详细信息,请参阅 Drop 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

    向表中添加行筛选器函数。 对表的所有后续查询都会收到函数计算结果为布尔值 TRUE 的行的子集。 这对于细粒度的访问控制目的非常有用,在这种情况下,该函数可以检查调用用户的身份或组成员身份,以决定是否筛选特定行。

  • DROP ROW FILTER

    适用于:勾选标记为“是” 仅 Unity Catalog

    从表中删除行筛选器(如果有)。 未来查询将返回表中的所有行,而无任何自动筛选。

  • SET TBLPROPERTIES

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

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

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

    > 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

      一个文本 STRINGtag_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

      一个文本 STRINGtag_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 表的聚类策略。

  • REPLACE PARTITIONED BY WITH CLUSTER BY [( <clustering_columns> ) | AUTO]

    适用于:检查标记为“是”的 Databricks SQL 检查标记为“是”的 Databricks Runtime 18.1 及更高版本

    将现有的已分区 Delta Lake 表转换为液体聚类分析,使读取器和编写器停机时间最小。 子 CLUSTER BY 句接受显式聚类分析列, AUTO 将键选择委托给预测优化,或者可以省略以使用现有分区列。

    请参阅 将分区表转换为液体聚类分析

  • { ENABLE | DISABLE | INHERIT } PREDICTIVE OPTIMIZATION

    适用于:勾选为“是”Databricks SQL 勾选为“是” Databricks Runtime 12.2 LTS 及更高版本 勾选标记为“是” 仅 Unity Catalog

    将托管 Delta Lake 表更改为所需的预测优化设置。

    默认情况下,创建表时,行为是从架构INHERIT

    当显式启用或继承为启用预测优化时,Azure Databricks 会根据其认为适当的方式自动在表上调用 OPTIMIZEVACUUM。 有关更多详细信息,请参阅:Unity Catalog 托管表的预测优化

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

其他示例

有关 Delta Lake 添加约束和更改列的示例,请参阅