ALTER TABLE (SQL Analytics)ALTER TABLE (SQL Analytics)

更改表的架构或属性。Alters the schema or properties of a table.

表标识符参数Table identifier parameter

所有语句中的表标识符参数都采用以下形式:The table identifier parameter in all statements has the following form:

  • table_identifiertable_identifier
    • [database_name.] table_name:表名,可选择使用数据库名称进行限定。[database_name.] table_name: A table name, optionally qualified with a database name.
    • delta.`<path-to-table>`:现有 Delta 表的位置。delta.`<path-to-table>`: The location of an existing Delta table.

ADD COLUMNSADD COLUMNS

向现有表添加列。Add columns to an existing table.

语法Syntax

ALTER TABLE table_identifier ADD COLUMNS ( col_spec [ , ... ] )

参数Parameters

  • COLUMNS ( col_spec )COLUMNS ( col_spec )

    要添加的列。The columns to be added.

ADD AND DROP PARTITIONADD AND DROP PARTITION

ADD PARTITIONADD PARTITION

将分区添加到分区表。Adds a partition to the partitioned table.

语法Syntax

ALTER TABLE table_identifier ADD [IF NOT EXISTS]
    ( partition_spec [ partition_spec ... ] )

参数Parameters

  • partition_specpartition_spec

    要添加的分区。Partition to be added.

    语法: PARTITION ( partition_col_name = partition_col_val [ , ... ] )Syntax: PARTITION ( partition_col_name = partition_col_val [ , ... ] )

DROP PARTITIONDROP PARTITION

删除表的分区。Drops the partition of the table.

语法Syntax

ALTER TABLE table_identifier DROP [ IF EXISTS ] partition_spec [PURGE]

参数Parameters

  • partition_specpartition_spec

    要删除的分区。Partition to be dropped.

    语法: PARTITION ( partition_col_name = partition_col_val [ , ... ] )Syntax: PARTITION ( partition_col_name = partition_col_val [ , ... ] )

RENAME TORENAME TO

更改数据库中现有表的名称。Changes the name of an existing table in the database.

语法Syntax

ALTER TABLE table_name RENAME TO table_name

ALTER TABLE table_identifier partition_spec RENAME TO partition_spec

参数Parameters

  • table_nametable_name

    表名,可选择使用数据库名称进行限定。A table name, optionally qualified with a database name.

    语法: [database_name.] table_nameSyntax: [database_name.] table_name

  • partition_specpartition_spec

    要重命名的分区。Partition to be renamed.

    语法: PARTITION ( partition_col_name = partition_col_val [ , ... ] )Syntax: PARTITION ( partition_col_name = partition_col_val [ , ... ] )

SET AND UNSETSET AND UNSET

SET TABLE PROPERTIESSET TABLE PROPERTIES

设置和取消设置表属性。Set and unset table properties. 如果已设置属性,将用新属性覆盖旧值。If a property was already set, overrides the old value with the new one.

语法Syntax

-- Set Table Properties
ALTER TABLE table_identifier SET TBLPROPERTIES ( key1 = val1, key2 = val2, ... )

-- Unset Table Properties
ALTER TABLE table_identifier UNSET TBLPROPERTIES [ IF EXISTS ] ( key1, key2, ... )

SET SERDESET SERDE

设置 Hive 表中的 SERDESERDE 属性。Sets the SERDE or SERDE properties in Hive tables. 如果已设置属性,将用新属性覆盖旧值。If a property was already set, overrides the old value with the new one.

语法Syntax

-- Set SERDE Properties
ALTER TABLE table_identifier [ partition_spec ]
    SET SERDEPROPERTIES ( key1 = val1, key2 = val2, ... )

ALTER TABLE table_identifier [ partition_spec ] SET SERDE serde_class_name
    [ WITH SERDEPROPERTIES ( key1 = val1, key2 = val2, ... ) ]

SET LOCATION And SET FILE FORMATSET LOCATION And SET FILE FORMAT

设置现有表的文件位置和文件格式。Sets the file location and file format for existing tables.

语法Syntax

-- Changing File Format
ALTER TABLE table_identifier [ partition_spec ] SET FILEFORMAT file_format

-- Changing File Location
ALTER TABLE table_identifier [ partition_spec ] SET LOCATION 'new_location'

参数Parameters

  • partition_specpartition_spec

    必须在其上设置属性的分区。The partition on which the property has to be set.

    语法: PARTITION ( partition_col_name = partition_col_val [ , ... ] )Syntax: PARTITION ( partition_col_name = partition_col_val [ , ... ] )

  • SERDEPROPERTIES ( key1 = val1, key2 = val2, … )SERDEPROPERTIES ( key1 = val1, key2 = val2, … )

    要设置的 SERDE 属性。The SERDE properties to be set.

Delta 表选项Delta table options

除标准 ALTER TABLE 选项外,Delta 表还支持本部分中介绍的选项。In addition to the standard ALTER TABLE options, Delta tables support the options described in this section.

本节内容:In this section:

有关添加、更改和替换列示例的详细说明,请参阅显式更新架构For add, change, and replace column examples, see Explicitly update schema.

ADD COLUMNSADD COLUMNS

将列(包括嵌套列)添加到现有表中。Adds columns to an existing table including adding nested columns. 如果表中已存在具有相同名称的列或同一嵌套结构,会引发异常。If a column with the same name already exists in the table or the same nested struct, an exception is thrown.

语法Syntax

ALTER TABLE table_identifier ADD COLUMNS (col_name data_type [COMMENT col_comment] [FIRST|AFTER colA_name], ...)

ALTER TABLE table_identifier ADD COLUMNS (col_name.nested_col_name data_type [COMMENT col_comment] [FIRST|AFTER colA_name], ...)

CHANGE COLUMNCHANGE COLUMN

更改现有表的列定义。Changes a column definition of an existing table. 可更改列的数据类型、注释或者为 Null 性,并对列重新排序。You can change the data type, comment, or nullability of a column and reorder columns.

语法Syntax

ALTER TABLE table_identifier (ALTER|CHANGE) [COLUMN] alterColumnAction

ALTER TABLE table_identifier (ALTER|CHANGE) [COLUMN] alterColumnAction

alterColumnAction:
    : TYPE dataType
    : [COMMENT col_comment]
    : [FIRST|AFTER colA_name]
    : (SET | DROP) NOT NULL

CHANGE COLUMN(Hive 语法)CHANGE COLUMN (Hive syntax)

更改现有表的列定义。Changes a column definition of an existing table. 可以更改列的注释并对列重新排序。You can change the comment of the column and reorder columns.

语法Syntax

ALTER TABLE table_identifier CHANGE [COLUMN] col_name col_name data_type [COMMENT col_comment] [FIRST|AFTER colA_name]

ALTER TABLE table_identifier CHANGE [COLUMN] col_name.nested_col_name col_name data_type [COMMENT col_comment] [FIRST|AFTER colA_name]

备注

不得使用 CHANGE COLUMNYou cannot use CHANGE COLUMN:

  • 要更改复杂数据类型(如结构)的内容。To change the contents of complex data types such as structs. 改用 ADD COLUMNS 来将新列添加到嵌套字段,或使用 ALTER COLUMN 更改嵌套列的属性。Instead use ADD COLUMNS to add new columns to nested fields, or ALTER COLUMN to change the properties of a nested column.
  • 若要放宽列的为 Null 性。To relax the nullability of a column. 请改用 ALTER TABLE table_name ALTER COLUMN column_name DROP NOT NULLInstead use ALTER TABLE table_name ALTER COLUMN column_name DROP NOT NULL.

REPLACE COLUMNSREPLACE COLUMNS

替换现有表的列定义。Replaces the column definitions of an existing table. 它支持更改列的注释、添加列和对列重新排序。It supports changing the comments of columns, adding columns, and reordering columns. 如果指定的列定义与现有定义不兼容,则引发异常。If specified column definitions are not compatible with the existing definitions, an exception is thrown.

语法Syntax

ALTER TABLE table_name REPLACE COLUMNS (col_name1 col_type1 [COMMENT col_comment1], ...)

示例Examples

-- RENAME table
DESC 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
DESC 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
DESC 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
DESC 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
DESC 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
DESC 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|
+-----------------------+---------+-----------+

-- Change the fileformat
ALTER TABLE loc_orc SET fileformat orc;

ALTER TABLE p1 partition (month=2, day=2) SET fileformat parquet;

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

-- SET SERDE/ SERDE Properties
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');

-- SET TABLE COMMENT Using SET PROPERTIES
ALTER TABLE dbx.tab1 SET TBLPROPERTIES ('comment' = 'A table comment.');

-- Alter TABLE COMMENT Using SET PROPERTIES
ALTER TABLE dbx.tab1 SET TBLPROPERTIES ('comment' = 'This is a new comment.');

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