适用于:
Databricks SQL
Databricks Runtime
更改表的架构或属性。
临时表不支持该 ALTER TABLE 命令。 如果 ALTER TABLE 命令应用于临时表,则返回错误。 如果已缓存表,则该命令将清除该表及其引用的所有依赖项的缓存数据。 在下次访问该表或这些依赖项时,将会延迟填充缓存。
在外表上,只能执行 ALTER TABLE SET OWNER 和 ALTER TABLE RENAME TO。
所需的权限
如果使用 Unity Catalog,则必须具有 MODIFY 权限:
ALTER COLUMNADD COLUMNDROP COLUMNSET TBLPROPERTIESUNSET TBLPROPERTIESPREDICTIVE 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}
参数
-
标识要更改的表。 名称不得包含 时态规范或选项规范。 如果找不到表,Azure Databricks 会引发 TABLE_OR_VIEW_NOT_FOUND 错误。
RENAME TOto_table_name重命名表。
-
标识新的表名称。 名称不得包含 时态规范或选项规范。
对于 Unity 目录表,该
to_table_name表必须位于同table_name一目录中。 对于其他表,to_table_name必须位于与table_name相同的模式中。如果
to_table_name不符合条件,则使用当前架构隐式限定。
> ALTER TABLE student RENAME TO student_info;-
-
向表中添加一个或多个列。
向现有 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';
-
更改属性或列的位置。
> 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 -
删除 Delta Lake 表中的一列或多列或字段。
-
重命名 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
-
向表添加 CHECK 约束、信息性外键约束或信息性主键约束。
仅 Unity Catalog 中的表支持外键和主键,而
hive_metastore目录不支持。 DEFAULT COLLATIONcollation_name适用于:
Databricks SQL
Databricks Runtime 16.3 及更高版本更改新
STRING列的表的默认排序规则。 现有列不受此子句的影响。 若要更改现有列的排序规则,请使用ALTER TABLE ... ALTER COLUMN ... COLLATE collation_name。
-
从表中删除主键、外键或 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_PERIOD 或 DELTA_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;-
向表中添加一个或多个分区。
> 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 -
从表中删除一个或多个分区。
> 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 -
设置分区的位置。
> ALTER TABLE dbx.tab1 PARTITION (a='1', b='2') SET LOCATION '/path/to/part/ways'; -
替换分区的键。
> 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 扫描表的位置,并将已直接添加到文件系统的任何文件添加到表中。
SETROW FILTER 条款适用于:
Databricks SQL
Databricks Runtime 12.2 LTS 及更高版本
仅 Unity Catalog向表中添加行筛选器函数。 对表的所有后续查询都会收到函数计算结果为布尔值 TRUE 的行的子集。 这对于细粒度的访问控制目的非常有用,在这种情况下,该函数可以检查调用用户的身份或组成员身份,以决定是否筛选特定行。
DROP ROW FILTER适用于:
仅 Unity Catalog从表中删除行筛选器(如果有)。 未来查询将返回表中的所有行,而无任何自动筛选。
-
设置或重置一个或多个用户定义的属性。
> ALTER TABLE dbx.tab1 SET TBLPROPERTIES ('winner' = 'loser'); -
移除一个或多个用户定义的属性。
> 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 pathLOCATION pathpath必须是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');-
适用于:
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。当显式启用或继承为启用预测优化时,Azure Databricks 会根据其认为适当的方式自动在表上调用 OPTIMIZE 和 VACUUM。 有关更多详细信息,请参阅:Unity Catalog 托管表的预测优化。
-- Enables predictive optimization for my_table > ALTER TABLE my_table ENABLE PREDICTIVE OPTIMIZATION;
其他示例
有关 Delta Lake 添加约束和更改列的示例,请参阅
相关文章
- ADD CONSTRAINT 子句:添加或删除 check 约束、主键和外键。
- ALTER MATERIALIZED VIEW:用于更改具体化视图的语法。
- ALTER STREAMING TABLE:用于更改流式处理表的语法。
- ALTER TABLE ... COLUMN 子句:ADD、ALTER、DROP 和 RENAME COLUMN的完整语法。
- ALTER TABLE … PARTITION:用于添加、删除和重命名分区的完整语法。
- 更新表架构:重命名或更改 Delta 表中的列类型。