Altering and Dropping Tables in Hive
Altering and Dropping Tables in Hive
The ALTER TABLE statement in Hive provides various functionalities to modify the structure and properties of an existing table. Renaming a Table.
Hive allows you to modify table structure and properties using the ALTER TABLE command.
Add new columns to an existing table (only appends at the end):
ALTER TABLE employees ADD COLUMNS (birth_date STRING, hire_date STRING);
⚡ Note: Hive does not support removing specific columns directly.
Rename a column or change its data type:
ALTER TABLE employees CHANGE COLUMN name full_name STRING;
✅ Make sure to specify the full column definition again when renaming.
Replace the entire schema (column list) of the table:
ALTER TABLE employees REPLACE COLUMNS (
emp_id INT,
full_name STRING,
salary DOUBLE
);
Update the HDFS location of a table (mostly for external tables):
ALTER TABLE customers SET LOCATION '/new/location/customers';
Convert table storage format to another type (e.g., from TEXTFILE to PARQUET):
ALTER TABLE employees SET FILEFORMAT PARQUET;
Add or update table properties:
ALTER TABLE employees SET TBLPROPERTIES ('comment'='Employee master table');
You can drop tables to remove them from the Hive Metastore.
Managed tables delete both metadata and data from the warehouse location:
DROP TABLE employees;
External tables only remove metadata — data files remain intact:
DROP TABLE customers;
⚠️ This is useful when you want to keep the raw data files but remove the table definition.
View table schema and details:
DESCRIBE FORMATTED employees;
Rename a table:
ALTER TABLE employees RENAME TO employees_archive;
Recover partitions if data is added manually:
MSCK REPAIR TABLE logs;
Use ALTER TABLE to modify schema, properties, location, or file format.
Use DROP TABLE carefully—managed tables delete data, external tables do not.
Always back up important data and Hive Metastore before making changes.