ALTER TABLE

    RENAME

    ALTER TABLE RENAME TO statement changes the table name of an existing table in the database. The table rename command cannot be used to move a table between databases, only to rename a table within the same database.

    If the table is cached, the commands clear cached data of the table. The cache will be lazily filled when the next time the table is accessed. Additionally:

    • the table rename command uncaches all table’s dependents such as views that refer to the table. The dependents should be cached again explicitly.
    • the partition rename command clears caches of all table dependents while keeping them as cached. So, their caches will be lazily filled when the next time they are accessed.

    Syntax

    Parameters

    • table_identifier

      Specifies a table name, which may be optionally qualified with a database name.

      Syntax: [ database_name. ] table_name

    • partition_spec

      Partition to be renamed. Note that one can use a typed literal (e.g., date’2019-01-02’) in the partition spec.

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

    ADD COLUMNS

    ALTER TABLE ADD COLUMNS statement adds mentioned columns to an existing table.

    Syntax

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

    Parameters

    • table_identifier

      Specifies a table name, which may be optionally qualified with a database name.

      Syntax: [ database_name. ] table_name

    • COLUMNS ( col_spec )

      Specifies the columns to be added.

    DROP COLUMNS

    ALTER TABLE DROP COLUMNS statement drops mentioned columns from an existing table. Note that this statement is only supported with v2 tables.

    Syntax

    1. ALTER TABLE table_identifier DROP { COLUMN | COLUMNS } [ ( ] col_name [ , ... ] [ ) ]

    Parameters

    • table_identifier

      Specifies a table name, which may be optionally qualified with a database name.

      Syntax: [ database_name. ] table_name

    • col_name

      Specifies the name of the column.

    ALTER TABLE RENAME COLUMN statement changes the column name of an existing table. Note that this statement is only supported with v2 tables.

    Syntax

    1. ALTER TABLE table_identifier RENAME COLUMN col_name TO col_name

    Parameters

    • table_identifier

      Specifies a table name, which may be optionally qualified with a database name.

      Syntax: [ database_name. ] table_name

    • col_name

    ALTER OR CHANGE COLUMN

    ALTER TABLE ALTER COLUMN or ALTER TABLE CHANGE COLUMN statement changes column’s definition.

    Syntax

    Parameters

    • table_identifier

      Specifies a table name, which may be optionally qualified with a database name.

      Syntax: [ database_name. ] table_name

    • col_name

      Specifies the name of the column.

    • alterColumnAction

      Change column’s definition.

    REPLACE COLUMNS

    ALTER TABLE REPLACE COLUMNS statement removes all existing columns and adds the new set of columns. Note that this statement is only supported with v2 tables.

    Syntax

    1. [ ( ] qualified_col_type_with_position_list [ ) ]

    Parameters

    • table_identifier

      Specifies a table name, which may be optionally qualified with a database name.

      Syntax: [ database_name. ] table_name

    • partition_spec

      Partition to be replaced. Note that one can use a typed literal (e.g., date’2019-01-02’) in the partition spec.

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

    • qualified_col_type_with_position_list

      The list of the column(s) to be added

      Syntax: col_name col_type [ col_comment ] [ col_position ] [ , ... ]

    ADD AND DROP PARTITION

    ADD PARTITION

    ALTER TABLE ADD statement adds partition to the partitioned table.

    If the table is cached, the command clears cached data of the table and all its dependents that refer to it. The cache will be lazily filled when the next time the table or the dependents are accessed.

    Syntax
    1. ALTER TABLE table_identifier ADD [IF NOT EXISTS]
    2. ( partition_spec [ partition_spec ... ] )
    Parameters
    • table_identifier

      Specifies a table name, which may be optionally qualified with a database name.

      Syntax: [ database_name. ] table_name

    • partition_spec

      Partition to be added. Note that one can use a typed literal (e.g., date’2019-01-02’) in the partition spec.

    DROP PARTITION

    ALTER TABLE DROP statement drops the partition of the table.

    If the table is cached, the command clears cached data of the table and all its dependents that refer to it. The cache will be lazily filled when the next time the table or the dependents are accessed.

    Syntax
    1. ALTER TABLE table_identifier DROP [ IF EXISTS ] partition_spec [PURGE]
    Parameters
    • table_identifier

      Specifies a table name, which may be optionally qualified with a database name.

      Syntax: [ database_name. ] table_name

    • partition_spec

      Partition to be dropped. Note that one can use a typed literal (e.g., date’2019-01-02’) in the partition spec.

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

    SET TABLE PROPERTIES

    ALTER TABLE SET command is used for setting the table properties. If a particular property was already set, this overrides the old value with the new one.

    ALTER TABLE UNSET is used to drop the table property.

    Syntax

    SET SERDE

    ALTER TABLE SET command is used for setting the SERDE or SERDE properties in Hive tables. If a particular property was already set, this overrides the old value with the new one.

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

    SET LOCATION And SET FILE FORMAT

    ALTER TABLE SET command can also be used for changing the file location and file format for existing tables.

    If the table is cached, the ALTER TABLE .. SET LOCATION command clears cached data of the table and all its dependents that refer to it. The cache will be lazily filled when the next time the table or the dependents are accessed.

    Syntax
    1. -- Changing File Format
    2. ALTER TABLE table_identifier [ partition_spec ] SET FILEFORMAT file_format
    3. -- Changing File Location
    4. ALTER TABLE table_identifier [ partition_spec ] SET LOCATION 'new_location'

    Parameters

    • table_identifier

      Specifies a table name, which may be optionally qualified with a database name.

      Syntax: [ database_name. ] table_name

    • partition_spec

      Specifies the partition on which the property has to be set. Note that one can use a typed literal (e.g., date’2019-01-02’) in the partition spec.

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

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

      Specifies the SERDE properties to be set.

    RECOVER PARTITIONS

    ALTER TABLE RECOVER PARTITIONS statement recovers all the partitions in the directory of a table and updates the Hive metastore. Another way to recover partitions is to use MSCK REPAIR TABLE.

    Syntax

    Parameters

    • table_identifier

      Specifies a table name, which may be optionally qualified with a database name.

      Syntax: [ database_name. ] table_name

    Examples