Renaming a column in Snowflake is a common and easy task to undertake by using the alter table
command, here is the simplest example of how to rename a column:
alter table table_name rename column old_column_name to new_column_name
Rename column in Snowflake if table exists already
We can build upon the simple example we showed previously by adding an if exists
constraint, which checks first if the table exists before renaming the column in the table. This is helpful as it stops potential errors being returned.
alter table if exists table_name rename column old_column_name to new_column_name
Renaming a column in Snowflake, things to watch out for
When renaming a column, there are three things to keep in mind:
- The new column name must not be currently used in the table
- Renaming the column does not update references to that table in other objects (such as view definitions), so this must be updated to the new name manually
- Columns that are part of the clustering key cannot be renamed
Other alter table commands
The alter table
command is the key to modifying the properties, columns, or constraints of a table in Snowflake. Other commands that you might want to work with are:
Add column to Snowflake table
Add multiple columns to Snowflake table