ALTER Statements

With Hive dialect, the following ALTER statements are supported for now:

  • ALTER DATABASE
  • ALTER TABLE
  • ALTER VIEW

ALTER DATABASE

Description

ALTER DATABASE statement is used to change the properties or location of a database.

Syntax

  1. -- alter database's properties
  2. ALTER (DATABASE|SCHEMA) database_name SET DBPROPERTIES (property_name=property_value, ...);
  3. -- alter database's localtion
  4. ALTER (DATABASE|SCHEMA) database_name SET LOCATION hdfs_path;

Synopsis

  • The uses of SCHEMA and DATABASE are interchangeable - they mean the same thing.
  • The ALTER DATABASE .. SET LOCATION statement is only supported in Hive-2.4.0 or later. The statement doesn’t move the contents of the database’s current directory to the newly specified location. It does not change the locations associated with any tables/partitions under the specified database. It only changes the default parent-directory where new tables will be added for this database. This behaviour is analogous to how changing a table-directory does not move existing partitions to a different location.

Examples

  1. -- alter database's properties
  2. ALTER DATABASE d1 SET DBPROPERTIES ('p1' = 'v1', 'p2' = 'v2');
  3. -- alter database's location
  4. ALTER DATABASE d1 SET LOCATION '/new/path';

ALTER TABLE

Description

ALTER TABLE statement changes the schema or properties of a table.

Rename Table

Description

The RENAME TABLE statement allows user to change the name of a table to a different name.

Syntax

  1. ALTER TABLE table_name RENAME TO new_table_name;

Examples

  1. ALTER TABLE t1 RENAME TO t2;

Alter Table Properties

Description

The ALTER TABLE PROPERTIES statement allows user add own metadata to tables. Currently, last_modified_user, last_modified_time properties are automatically added and managed by Hive.

Syntax

  1. ALTER TABLE table_name SET TBLPROPERTIES table_properties;
  2. table_properties:
  3. : (property_name = property_value, property_name = property_value, ... )

Examples

  1. ALTER TABLE t1 SET TBLPROPERTIES ('p1' = 'v1', 'p2' = 'v2');

Add / Remove SerDe Properties

Description

The statement enable user to change a table’s SerDe or add/move user-defined metadata to the table’s SerDe Object. The SerDe properties are passed to the table’s SerDe to serialize and deserialize data. So users can store any information required for their custom SerDe here. Refer to the Hive’s SerDe docs and Hive SerDe for more details.

Syntax

Add SerDe Properties:

  1. ALTER TABLE table_name [PARTITION partition_spec] SET SERDE serde_class_name [WITH SERDEPROPERTIES serde_properties];
  2. ALTER TABLE table_name [PARTITION partition_spec] SET SERDEPROPERTIES serde_properties;
  3. serde_properties:
  4. : (property_name = property_value, property_name = property_value, ... )

Remove SerDe Properties:

  1. ALTER TABLE table_name [PARTITION partition_spec] UNSET SERDEPROPERTIES (property_name, ... );

Examples

  1. -- add serde properties
  2. ALTER TABLE t1 SET SERDEPROPERTIES ('field.delim' = ',');
  3. -- remove serde properties
  4. ALTER TABLE t1 UNSET SERDEPROPERTIES ('field.delim');

Alter Partition

ALTER TABLE ... PARTITION .. statement is used to add/rename/drop partitions.

Add Partitions

ALTER TABLE .. ADD PARTITION statement is used to add partitions. Partition values should be quoted only if they are strings. The location must be a directory inside which data files reside. (ADD PARTITION changes the table metadata, but does not load data. If the data does not exist in the partition’s location, queries will not return any results.) An error is thrown if the partition_spec for the table already exists. You can use IF NOT EXISTS to skip the error.

Syntax
  1. ALTER TABLE table_name ADD [IF NOT EXISTS]
  2. PARTITION partition_spec [LOCATION 'location']
  3. [, PARTITION partition_spec [LOCATION 'location'], ...];
  4. partition_spec:
  5. : (partition_column = partition_col_value, partition_column = partition_col_value, ...)
Examples
  1. ALTER TABLE t1 ADD PARTITION (dt='2022-08-08', country='china') location '/path/to/us/part080808'
  2. PARTITION (dt='2022-08-09', country='china') location '/path/to/us/part080809';

Rename Partitions

ALTER TABLE .. PARTITION ... RENAME TO ... statement is used to rename partition.

Syntax
  1. ALTER TABLE table_name PARTITION partition_spec RENAME TO PARTITION partition_spec;
Examples
  1. ALTER TABLE t1 PARTITION (dt='2022-08-08', country='china')
  2. RENAME TO PARTITION (dt='2023-08-08', country='china');

Drop Partitions

ALTER TABLE .. DROP PARTITION ... statement is used to drop partition. This removes the data and metadata for this partition. The data is actually moved to the .Trash/Current directory if Trash is configured, but the metadata is completed lost.

Syntax
  1. ALTER TABLE table_name DROP [IF EXISTS] PARTITION partition_spec[, PARTITION partition_spec, ...]
Examples
  1. ALTER TABLE t1 DROP IF EXISTS PARTITION (dt='2022-08-08', country='china');

Alter Location / File Format

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

Syntax
  1. --- Alter File Location
  2. ALTER TABLE table_name [PARTITION partition_spec] SET LOCATION "new location";
  3. --- Alter File Format
  4. ALTER TABLE table_name [PARTITION partition_spec] SET FILEFORMAT file_format;
Examples
  1. -- alter file localtion
  2. ALTER TABLE t1 PARTITION (dt='2022-08-08', country='china') SET LOCATION "/user/warehouse/t2/dt=2022-08-08/country=china";
  3. -- alter file format
  4. ALTER TABLE t1 PARTITION (dt='2022-08-08', country='china') SET FILEFORMAT ORC;

Alter Column

Rules for Column Names

Column names are case-insensitive. Backtick quotation enables the use of reserved keywords for column names, as well as table names.

Change Column’s Definition

The statement allow users to change a column’s name, data type, comment, or position, or an arbitrary combination of them.

Syntax
  1. ALTER TABLE table_name [PARTITION partition_spec] CHANGE [COLUMN] col_old_name col_new_name column_type
  2. [COMMENT col_comment] [FIRST|AFTER column_name] [CASCADE|RESTRICT];
Examples
  1. ALTER TABLE t1 CHANGE COLUMN c1 new_c1 STRING FIRST;
  2. ALTER TABLE t1 CHANGE COLUMN c1 new_c1 STRING AFRER c2;

Add/Replace Columns

The statement allow users to add new columns or replace the existing columns with the new columns.

Syntax
  1. ALTER TABLE table_name
  2. [PARTITION partition_spec]
  3. ADD|REPLACE COLUMNS (col_name data_type [COMMENT col_comment], ...)
  4. [CASCADE|RESTRICT]

ADD COLUMNS will add new columns to the end of the existing columns before the partition columns.

REPLACE COLUMNS will remove all existing columns and add the new set of columns.

Synopsis

ALTER TABLE ... COLUMNS with CASCADE command changes the columns of a table’s metadata, and cascades the same change to all the partition metadata. RESTRICT is the default, limiting column changes only to table metadata.

Examples
  1. -- add column
  2. ALTER TABLE t1 ADD COLUMNS (ch CHAR(5), name STRING) CASCADE;
  3. -- replace column
  4. ALTER TABLE t1 REPLACE COLUMNS (t1 TINYINT, d DECIMAL) CASCADE;

ALTER VIEW

Alter View Properties

ALTER VIEW ... SET TBLPROPERTIES .. allow user to add own metadata to a view.

Syntax

  1. ALTER VIEW [db_name.]view_name SET TBLPROPERTIES table_properties;
  2. table_properties:
  3. : (property_name = property_value, property_name = property_value, ...)

Examples

  1. ALTER VIEW v1 SET TBLPROPERTIES ('p1' = 'v1');

Alter View As Select

ALTER VIEW ... AS .. allow user to change the definition of a view, which must exist.

Syntax

  1. ALTER VIEW [db_name.]view_name AS select_statement;

Examples

  1. ALTER VIEW v1 AS SELECT * FROM t2;