適用於:
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 OWNER
和 ALTER TABLE RENAME TO
。
如果您使用 Unity Catalog ,則必須具有以下項目的 MODIFY
權限:
- ALTER COLUMN
- ADD COLUMN
- DROP COLUMN
-
SET TBLPROPERTIES
- UNSET TBLPROPERTIES
- modify PREDICTIVE OPTIMIZATION
如果您使用 Unity Catalog,您必須擁有 MANAGE
許可權或擁有下列所有權:
執行所有其他作業需要具有資料表的所有權。
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}
ADD CONSTRAINT
將檢查約束、資訊性外鍵約束或資訊性主鍵約束新增至資料表。
只有 Unity Catalog 中的資料表支援外部索引鍵和主索引鍵,而不是 hive_metastore
目錄。
DEFAULT COLLATION
collation_name
Applies to:
Databricks Runtime 16.3 and above
變更數據表中新 STRING
欄位的預設排序規則。
現有的欄位不會受到這個子句的影響。
若要變更現有資料列的定序,請使用 ALTER TABLE ... ALTER COLUMN ... COLLATE collation_name
。
DROP CONSTRAINT
Drops a primary key, foreign key, or check constraint from the table.
DROP FEATURE feature_name [ TRUNCATE HISTORY ]
適用於:
Databricks SQL
Databricks Runtime 14.1 和更新版本
從 Delta Lake 資料表中移除功能。
移除影響讀取器和寫入器的功能需要一個兩階段處理過程:
詳情請參閱 協議相容性的表格功能。
feature_name
以 STRING
常值或識別碼形式表示的功能名稱,必須被 Azure Databricks 理解,並且在資料表上受到支援。
支援的 feature_names
為:
- ‘deletionVectors’ or
deletionvectors
- ‘v2Checkpoint’ 或
v2checkpoint
如果 Azure Databricks 資料表中沒有此功能,就會引發 DELTA_FEATURE_DROP_FEATURE_NOT_PRESENT。
截短歷史
Optionally allows you to initiate the second phase of dropping a reader-plus-writer feature after 24 hours by truncating the table history to when the invocation command was executed.
截斷數據表歷程記錄會限制您執行 DESCRIBE HISTORY 和執行時間移動查詢的能力。
加 PARTITION
將一個或多個分割區新增至資料表。
DROP PARTITION
從資料表刪除一個或多個分割區。
PARTITION … SET LOCATION
設定分割區的位置。
重新命名 PARTITION
Replaces the keys of a partition.
RECOVER PARTITIONS
指示 Azure Databricks 掃描資料表的位置,並將任何檔案新增至已直接新增至檔案系統的資料表。
SET
ROW FILTER 條款
適用於:
Databricks SQL
Databricks Runtime 12.2 LTS 和更新版本
僅限 Unity Catalog
將資料列篩選函數新增至資料表。 All subsequent queries to the table receive a subset of the rows where the function evaluates to boolean TRUE. 這適用於更細緻的存取控制,其中函式可以檢查叫用使用者的身分識別或群組成員資格,以決定是否要篩選某些資料列。
DROP ROW FILTER
Applies to:
Unity Catalog only
Drops the row filter from the table, if any.
未來的查詢將會從資料表傳回所有資料列,而不需要任何自動篩選。
SET TBLPROPERTIES
設定或重設一個或多個使用者定義的屬性。
UNSET TBLPROPERTIES
移除一個或多個使用者定義的屬性。
SET LOCATION
移動資料表的位置。
SET LOCATION path
[ 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
權限,才能將標籤新增至資料表。
UNSET TAGS ( tag_name [, ...] )
適用於:
Databricks SQL
Databricks Runtime 13.3 LTS 和更新版本
從資料表中移除標籤。 您需要具有 APPLY TAG
權限,才能從資料表中移除標籤。
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 資料表變更為所需的預測優化設定。
By default, when tables are created, the behavior is to INHERIT
from the schema.
When predictive optimization is explicitly enabled or inherited as enabled OPTIMIZE and VACUUM will be automatically invoked on the table as deemed appropriate by Azure Databricks. 如需詳細資訊,請參閱 Unity Catalog 受控資料表的預測性最佳化。
For Delta Lake add constraints and alter column examples, see
-- 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