ALTER TABLE

Alters the schema or properties of a table.

Table identifier parameter

The table identifier parameter in all statements has the following form:

  • table_identifier
    • [database_name.] table_name: A table name, optionally qualified with a database name.
    • delta.`<path-to-table>` : The location of an existing Delta table.

ADD COLUMNS

Adds columns to an existing table.

Syntax

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

For ADD COLUMNS syntax specific to Delta tables, see Delta table schema options.

Parameters

  • COLUMNS ( col_spec )

    The columns to be added.

ADD AND DROP PARTITION

ADD PARTITION

Adds a partition to the partitioned table.

Syntax

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

Parameters

  • partition_spec

    Partition to be added.

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

DROP PARTITION

Drops the partition of the table.

Syntax

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

Parameters

  • partition_spec

    Partition to be dropped.

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

  • IF EXISTS

    Do not throw an exception if the partition does not exist.

RENAME 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_name

    A table name, optionally qualified with a database name.

    Syntax: [database_name.] table_name

  • partition_spec

    Partition to be renamed.

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

SET AND UNSET

SET TABLE PROPERTIES

Sets and unsets 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, ... )

Parameters

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

    The table properties to be set or unset.

  • IF EXISTS

    Do not throw an exception if the property does not exist.

Example: Set a table comment

To set a table comment, run:

ALTER TABLE table_name SET TBLPROPERTIES ('comment' = 'A table comment.')

SET SERDE

Sets the SERDE or SERDEPROPERTIES 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, ... ) ]

Parameters

  • serde_class_name

    The fully-qualified path to the SerDe classname.

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

    The SerDe properties to be set.

SET LOCATION

Changes the location of the table definition. Files are moved to the new location, not copied.

Syntax

-- Change file location
ALTER TABLE table_identifier [ partition_spec ] SET LOCATION 'new_location'

Parameters

  • partition_spec

    The partition on which the property has to be set.

  • new_location

    The path to the location of the table files.

Delta table schema options

In addition to the standard ALTER TABLE options, Delta tables support the options described in this section.

ADD 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], ...)

For add columns examples, see Add columns.

CHANGE COLUMN

Changes a column definition of an existing table. 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
  • DROP NOT NULL is available in Databricks Runtime 7.0 and above.
  • SET NOT NULL is available in Databricks Runtime 7.4 and above.

Note

  • You can change a column type only in Delta tables.
  • You can change the type from Byte -> Short -> Integer without a data rewrite. All other type changes require you to rewrite the data.

For change column examples, see Change column comment or ordering. Also see Constraints.

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]

Note

In Databricks Runtime 7.0 and above you cannot use CHANGE COLUMN:

  • To change the contents of complex data types such as structs. Instead use ADD COLUMNS to add new columns to nested fields, or ALTER COLUMN to change the properties of a nested column.
  • To relax the nullability of a column. Instead use ALTER TABLE table_name ALTER COLUMN column_name DROP NOT NULL.

REPLACE 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], ...)

For replace columns examples, see Replace columns.

ADD CONSTRAINT

Note

Available in Databricks Runtime 7.4 and above.

Adds a constraint that a must be true for each input row in a table.

For an example, see CHECK constraint.

Syntax

ALTER TABLE table_identifier ADD CONSTRAINT constraint_name CHECK condition
  • constraint_name

    The name of a constraint to add.

  • condition

    A Boolean expression that must be true for each input row.

DROP CONSTRAINT

Drops a constraint from the table.

Note

Available in Databricks Runtime 7.4 and above.

Syntax

ALTER TABLE table_identifier DROP CONSTRAINT constraint_name
  • constraint_name

    The name of a constraint to drop.

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 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');