ALTER TABLE

Description

ALTER TABLE statement changes the schema or properties of a 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

  1. ALTER TABLE table_identifier RENAME TO table_identifier
  2. ALTER TABLE table_identifier partition_spec RENAME TO 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 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.

RENAME 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

    Specifies the name of the column.

ALTER OR CHANGE COLUMN

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

Syntax

  1. ALTER TABLE table_identifier { ALTER | CHANGE } [ COLUMN ] col_name alterColumnAction

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. ALTER TABLE table_identifier [ partition_spec ] REPLACE COLUMNS
  2. [ ( ] 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.

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

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 AND UNSET

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
  1. -- Set Table Properties
  2. ALTER TABLE table_identifier SET TBLPROPERTIES ( key1 = val1, key2 = val2, ... )
  3. -- Unset Table Properties
  4. ALTER TABLE table_identifier UNSET TBLPROPERTIES [ IF EXISTS ] ( key1, key2, ... )

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. -- Set SERDE Properties
  2. ALTER TABLE table_identifier [ partition_spec ]
  3. SET SERDEPROPERTIES ( key1 = val1, key2 = val2, ... )
  4. ALTER TABLE table_identifier [ partition_spec ] SET SERDE serde_class_name
  5. [ 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

  1. ALTER TABLE table_identifier RECOVER PARTITIONS

Parameters

  • table_identifier

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

    Syntax: [ database_name. ] table_name

Examples

  1. -- RENAME table
  2. DESC student;
  3. +-----------------------+---------+-------+
  4. | col_name|data_type|comment|
  5. +-----------------------+---------+-------+
  6. | name| string| NULL|
  7. | rollno| int| NULL|
  8. | age| int| NULL|
  9. |# Partition Information| | |
  10. | # col_name|data_type|comment|
  11. | age| int| NULL|
  12. +-----------------------+---------+-------+
  13. ALTER TABLE Student RENAME TO StudentInfo;
  14. -- After Renaming the table
  15. DESC StudentInfo;
  16. +-----------------------+---------+-------+
  17. | col_name|data_type|comment|
  18. +-----------------------+---------+-------+
  19. | name| string| NULL|
  20. | rollno| int| NULL|
  21. | age| int| NULL|
  22. |# Partition Information| | |
  23. | # col_name|data_type|comment|
  24. | age| int| NULL|
  25. +-----------------------+---------+-------+
  26. -- RENAME partition
  27. SHOW PARTITIONS StudentInfo;
  28. +---------+
  29. |partition|
  30. +---------+
  31. | age=10|
  32. | age=11|
  33. | age=12|
  34. +---------+
  35. ALTER TABLE default.StudentInfo PARTITION (age='10') RENAME TO PARTITION (age='15');
  36. -- After renaming Partition
  37. SHOW PARTITIONS StudentInfo;
  38. +---------+
  39. |partition|
  40. +---------+
  41. | age=11|
  42. | age=12|
  43. | age=15|
  44. +---------+
  45. -- Add new columns to a table
  46. DESC StudentInfo;
  47. +-----------------------+---------+-------+
  48. | col_name|data_type|comment|
  49. +-----------------------+---------+-------+
  50. | name| string| NULL|
  51. | rollno| int| NULL|
  52. | age| int| NULL|
  53. |# Partition Information| | |
  54. | # col_name|data_type|comment|
  55. | age| int| NULL|
  56. +-----------------------+---------+-------+
  57. ALTER TABLE StudentInfo ADD columns (LastName string, DOB timestamp);
  58. -- After Adding New columns to the table
  59. DESC StudentInfo;
  60. +-----------------------+---------+-------+
  61. | col_name|data_type|comment|
  62. +-----------------------+---------+-------+
  63. | name| string| NULL|
  64. | rollno| int| NULL|
  65. | LastName| string| NULL|
  66. | DOB|timestamp| NULL|
  67. | age| int| NULL|
  68. |# Partition Information| | |
  69. | # col_name|data_type|comment|
  70. | age| int| NULL|
  71. +-----------------------+---------+-------+
  72. -- Drop columns of a table
  73. DESC StudentInfo;
  74. +-----------------------+---------+-------+
  75. | col_name|data_type|comment|
  76. +-----------------------+---------+-------+
  77. | name| string| NULL|
  78. | rollno| int| NULL|
  79. | LastName| string| NULL|
  80. | DOB|timestamp| NULL|
  81. | age| int| NULL|
  82. |# Partition Information| | |
  83. | # col_name|data_type|comment|
  84. | age| int| NULL|
  85. +-----------------------+---------+-------+
  86. ALTER TABLE StudentInfo DROP columns (LastName, DOB);
  87. -- After dropping columns of the table
  88. DESC StudentInfo;
  89. +-----------------------+---------+-------+
  90. | col_name|data_type|comment|
  91. +-----------------------+---------+-------+
  92. | name| string| NULL|
  93. | rollno| int| NULL|
  94. | age| int| NULL|
  95. |# Partition Information| | |
  96. | # col_name|data_type|comment|
  97. | age| int| NULL|
  98. +-----------------------+---------+-------+
  99. -- Rename a column of a table
  100. DESC StudentInfo;
  101. +-----------------------+---------+-------+
  102. | col_name|data_type|comment|
  103. +-----------------------+---------+-------+
  104. | name| string| NULL|
  105. | rollno| int| NULL|
  106. | age| int| NULL|
  107. |# Partition Information| | |
  108. | # col_name|data_type|comment|
  109. | age| int| NULL|
  110. +-----------------------+---------+-------+
  111. ALTER TABLE StudentInfo RENAME COLUMN name TO FirstName;
  112. -- After renaming a column of the table
  113. DESC StudentInfo;
  114. +-----------------------+---------+-------+
  115. | col_name|data_type|comment|
  116. +-----------------------+---------+-------+
  117. | FirstName| string| NULL|
  118. | rollno| int| NULL|
  119. | age| int| NULL|
  120. |# Partition Information| | |
  121. | # col_name|data_type|comment|
  122. | age| int| NULL|
  123. +-----------------------+---------+-------+
  124. -- ALTER OR CHANGE COLUMNS
  125. DESC StudentInfo;
  126. +-----------------------+---------+-------+
  127. | col_name|data_type|comment|
  128. +-----------------------+---------+-------+
  129. | FirstName| string| NULL|
  130. | rollno| int| NULL|
  131. | age| int| NULL|
  132. |# Partition Information| | |
  133. | # col_name|data_type|comment|
  134. | age| int| NULL|
  135. +-----------------------+---------+-------+
  136. ALTER TABLE StudentInfo ALTER COLUMN FirstName COMMENT "new comment";
  137. -- After ALTER or CHANGE COLUMNS
  138. DESC StudentInfo;
  139. +-----------------------+---------+-----------+
  140. | col_name|data_type| comment|
  141. +-----------------------+---------+-----------+
  142. | FirstName| string|new comment|
  143. | rollno| int| NULL|
  144. | age| int| NULL|
  145. |# Partition Information| | |
  146. | # col_name|data_type| comment|
  147. | age| int| NULL|
  148. +-----------------------+---------+-----------+
  149. -- REPLACE COLUMNS
  150. DESC StudentInfo;
  151. +-----------------------+---------+-----------+
  152. | col_name|data_type| comment|
  153. +-----------------------+---------+-----------+
  154. | FirstName| string|new comment|
  155. | rollno| int| NULL|
  156. | age| int| NULL|
  157. |# Partition Information| | |
  158. | # col_name|data_type| comment|
  159. | age| int| NULL|
  160. +-----------------------+---------+-----------+
  161. ALTER TABLE StudentInfo REPLACE COLUMNS (name string, ID int COMMENT 'new comment');
  162. -- After replacing COLUMNS
  163. DESC StudentInfo;
  164. +-----=---------+---------+-----------+
  165. | col_name|data_type| comment|
  166. +---------------+---------+-----------+
  167. | name| string| NULL|
  168. | ID| int|new comment|
  169. | # Partitioning| | |
  170. |Not partitioned| | |
  171. +---------------+---------+-----------+
  172. -- Add a new partition to a table
  173. SHOW PARTITIONS StudentInfo;
  174. +---------+
  175. |partition|
  176. +---------+
  177. | age=11|
  178. | age=12|
  179. | age=15|
  180. +---------+
  181. ALTER TABLE StudentInfo ADD IF NOT EXISTS PARTITION (age=18);
  182. -- After adding a new partition to the table
  183. SHOW PARTITIONS StudentInfo;
  184. +---------+
  185. |partition|
  186. +---------+
  187. | age=11|
  188. | age=12|
  189. | age=15|
  190. | age=18|
  191. +---------+
  192. -- Drop a partition from the table
  193. SHOW PARTITIONS StudentInfo;
  194. +---------+
  195. |partition|
  196. +---------+
  197. | age=11|
  198. | age=12|
  199. | age=15|
  200. | age=18|
  201. +---------+
  202. ALTER TABLE StudentInfo DROP IF EXISTS PARTITION (age=18);
  203. -- After dropping the partition of the table
  204. SHOW PARTITIONS StudentInfo;
  205. +---------+
  206. |partition|
  207. +---------+
  208. | age=11|
  209. | age=12|
  210. | age=15|
  211. +---------+
  212. -- Adding multiple partitions to the table
  213. SHOW PARTITIONS StudentInfo;
  214. +---------+
  215. |partition|
  216. +---------+
  217. | age=11|
  218. | age=12|
  219. | age=15|
  220. +---------+
  221. ALTER TABLE StudentInfo ADD IF NOT EXISTS PARTITION (age=18) PARTITION (age=20);
  222. -- After adding multiple partitions to the table
  223. SHOW PARTITIONS StudentInfo;
  224. +---------+
  225. |partition|
  226. +---------+
  227. | age=11|
  228. | age=12|
  229. | age=15|
  230. | age=18|
  231. | age=20|
  232. +---------+
  233. -- Change the fileformat
  234. ALTER TABLE loc_orc SET fileformat orc;
  235. ALTER TABLE p1 partition (month=2, day=2) SET fileformat parquet;
  236. -- Change the file Location
  237. ALTER TABLE dbx.tab1 PARTITION (a='1', b='2') SET LOCATION '/path/to/part/ways'
  238. -- SET SERDE/ SERDE Properties
  239. ALTER TABLE test_tab SET SERDE 'org.apache.hadoop.hive.serde2.columnar.LazyBinaryColumnarSerDe';
  240. ALTER TABLE dbx.tab1 SET SERDE 'org.apache.hadoop' WITH SERDEPROPERTIES ('k' = 'v', 'kay' = 'vee')
  241. -- SET TABLE PROPERTIES
  242. ALTER TABLE dbx.tab1 SET TBLPROPERTIES ('winner' = 'loser');
  243. -- SET TABLE COMMENT Using SET PROPERTIES
  244. ALTER TABLE dbx.tab1 SET TBLPROPERTIES ('comment' = 'A table comment.');
  245. -- Alter TABLE COMMENT Using SET PROPERTIES
  246. ALTER TABLE dbx.tab1 SET TBLPROPERTIES ('comment' = 'This is a new comment.');
  247. -- DROP TABLE PROPERTIES
  248. ALTER TABLE dbx.tab1 UNSET TBLPROPERTIES ('winner');
  249. -- RECOVER PARTITIONS
  250. ALTER TABLE dbx.tab1 RECOVER PARTITIONS;