Project: Apache Spark (App Definition and Development / Streaming & Messaging)
File: sql-ref-syntax-ddl-alter-table.md

layout: global title: ALTER TABLE displayTitle: ALTER TABLE license: | Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. `ALTER TABLE` statement changes the schema or properties of a table. `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. ```sql ALTER TABLE tableidentifier RENAME TO tableidentifier ALTER TABLE tableidentifier partitionspec RENAME TO partition_spec ``` table_identifier* Specifies a table name, which may be optionally qualified with a database name. Syntax: `[ databasename. ] tablename` 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 ( partitioncolname = partitioncolval [ , ... ] )` `ALTER TABLE ADD COLUMNS` statement adds mentioned columns to an existing table. ```sql ALTER TABLE tableidentifier ADD COLUMNS ( colspec [ , ... ] ) ``` table_identifier* Specifies a table name, which may be optionally qualified with a database name. Syntax: `[ databasename. ] tablename` COLUMNS ( col_spec )* Specifies the columns to be added. `ALTER TABLE DROP COLUMNS` statement drops mentioned columns from an existing table. Note that this statement is only supported with v2 tables. ```sql ALTER TABLE tableidentifier DROP { COLUMN | COLUMNS } [ ( ] colname [ , ... ] [ ) ] ``` table_identifier* Specifies a table name, which may be optionally qualified with a database name. Syntax: `[ databasename. ] tablename` 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. ```sql ALTER TABLE tableidentifier RENAME COLUMN colname TO col_name ``` table_identifier* Specifies a table name, which may be optionally qualified with a database name. Syntax: `[ databasename. ] tablename` col_name* Specifies the name of the column. `ALTER TABLE ALTER COLUMN` or `ALTER TABLE CHANGE COLUMN` statement changes column's

```sql ALTER TABLE tableidentifier { ALTER | CHANGE } [ COLUMN ] colname alterColumnAction ``` table_identifier* Specifies a table name, which may be optionally qualified with a database name. Syntax: `[ databasename. ] tablename` col_name* Specifies the name of the column. alterColumnAction* Change column's definition. `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. ```sql ALTER TABLE tableidentifier [ partitionspec ] REPLACE COLUMNS [ ( ] qualifiedcoltypewithposition_list [ ) ] ``` table_identifier* Specifies a table name, which may be optionally qualified with a database name. Syntax: `[ databasename. ] tablename` 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 ( partitioncolname = partitioncolval [ , ... ] )` qualified_col_type_with_position_list* The list of the column(s) to be added Syntax: `colname coltype [ colcomment ] [ colposition ] [ , ... ]` `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. ```sql ALTER TABLE table_identifier ADD [IF NOT EXISTS] ( partitionspec [ partitionspec ... ] ) ``` table_identifier* Specifies a table name, which may be optionally qualified with a database name. Syntax: `[ databasename. ] tablename` partition_spec* Partition to be added. Note that one can use a typed literal (e.g., date'2019-01-02') in the partition spec. Syntax: `PARTITION ( partitioncolname = partitioncolval [ , ... ] )` `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. ```sql ALTER TABLE tableidentifier DROP [ IF EXISTS ] partitionspec [PURGE] ``` table_identifier* Specifies a table name, which may be optionally qualified with a database name. Syntax: `[ databasename. ] tablename` 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 ( partitioncolname = partitioncolval [ , ... ] )` `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. ```sql -- 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, ... ) ``` `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. ```sql -- Set SERDE Properties ALTER TABLE tableidentifier [ partitionspec ] SET SERDEPROPERTIES ( key1 = val1, key2 = val2, ... ) ALTER TABLE tableidentifier [ partitionspec ] SET SERDE serdeclassname [ WITH SERDEPROPERTIES ( key1 = val1, key2 = val2,

) ] ``` `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. ```sql -- Changing File Format ALTER TABLE tableidentifier [ partitionspec ] SET FILEFORMAT file_format -- Changing File Location ALTER TABLE tableidentifier [ partitionspec ] SET LOCATION 'new_location' ``` table_identifier* Specifies a table name, which may be optionally qualified with a database name. Syntax: `[ databasename. ] tablename` 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 ( partitioncolname = partitioncolval [ , ... ] )` SERDEPROPERTIES ( key1 = val1, key2 = val2, ... )* Specifies the SERDE properties to be set. `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`. ```sql ALTER TABLE table_identifier RECOVER PARTITIONS ``` table_identifier* Specifies a table name, which may be optionally qualified with a database name. Syntax: `[ databasename. ] tablename` ```sql -- RENAME table DESC student; +--++-+ | colname|datatype|comment| +--++-+ | name| string| NULL| | rollno| int| NULL| | age| int| NULL| |# Partition Information| | | | # colname|datatype|comment| | age| int| NULL| +--++-+ ALTER TABLE Student RENAME TO StudentInfo; -- After Renaming the table DESC StudentInfo; +--++-+ | colname|datatype|comment| +--++-+ | name| string| NULL| | rollno| int| NULL| | age| int| NULL| |# Partition Information| | | | # colname|datatype|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; +--++-+ | colname|datatype|comment| +--++-+ | name| string| NULL| | rollno| int| NULL| | age| int| NULL| |# Partition Information| | | | # colname|datatype|comment| | age| int| NULL| +--++-+ ALTER TABLE StudentInfo ADD columns (LastName string, DOB timestamp); -- After Adding New columns to the table DESC StudentInfo; +--++-+ | colname|datatype|comment| +--++-+ | name| string| NULL| | rollno| int| NULL| | LastName| string| NULL| | DOB|timestamp| NULL| | age| int| NULL| |# Partition Information| | | | # colname|datatype|comment| | age| int| NULL| +--++-+ -- Drop columns of a table DESC StudentInfo; +--++-+ | colname|datatype|comment| +--++-+ | name| string| NULL| | rollno| int| NULL| | LastName| string| NULL| | DOB|timestamp| NULL| | age| int| NULL| |# Partition Information| | | | # colname|datatype|comment| | age| int| NULL| +--++-+ ALTER TABLE StudentInfo DROP columns (LastName, DOB); -- After dropping columns of the table DESC StudentInfo; +--++-+ | colname|datatype|comment| +--++-+ | name| string| NULL| | rollno| int| NULL| | age| int| NULL| |# Partition Information| | |

# colname|datatype|comment| | age| int| NULL| +--++-+ -- Rename a column of a table DESC StudentInfo; +--++-+ | colname|datatype|comment| +--++-+ | name| string| NULL| | rollno| int| NULL| | age| int| NULL| |# Partition Information| | | | # colname|datatype|comment| | age| int| NULL| +--++-+ ALTER TABLE StudentInfo RENAME COLUMN name TO FirstName; -- After renaming a column of the table DESC StudentInfo; +--++-+ | colname|datatype|comment| +--++-+ | FirstName| string| NULL| | rollno| int| NULL| | age| int| NULL| |# Partition Information| | | | # colname|datatype|comment| | age| int| NULL| +--++-+ -- ALTER OR CHANGE COLUMNS DESC StudentInfo; +--++-+ | colname|datatype|comment| +--++-+ | FirstName| string| NULL| | rollno| int| NULL| | age| int| NULL| |# Partition Information| | | | # colname|datatype|comment| | age| int| NULL| +--++-+ ALTER TABLE StudentInfo ALTER COLUMN FirstName COMMENT "new comment"; -- After ALTER or CHANGE COLUMNS DESC StudentInfo; +--++--+ | colname|datatype| comment| +--++--+ | FirstName| string|new comment| | rollno| int| NULL| | age| int| NULL| |# Partition Information| | | | # colname|datatype| comment| | age| int| NULL| +--++--+ -- REPLACE COLUMNS DESC StudentInfo; +--++--+ | colname|datatype| comment| +--++--+ | FirstName| string|new comment| | rollno| int| NULL| | age| int| NULL| |# Partition Information| | | | # colname|datatype| comment| | age| int| NULL| +--++--+ ALTER TABLE StudentInfo REPLACE COLUMNS (name string, ID int COMMENT 'new comment'); -- After replacing COLUMNS DESC StudentInfo; +--=++--+ | colname|datatype| comment| +++--+ | name| string| NULL| | ID| int|new comment| | # Partitioning| | | |Not partitioned| | | +++--+ -- 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| ++ -- 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'); -- RECOVER PARTITIONS ALTER TABLE dbx.tab1 RECOVER PARTITIONS; ```