Rename table in Snowflake

Rename table in Snowflake, simply explained

Renaming a table in Snowflake is a common and easy task to undertake by using the alter table command.

Stephen Allwright
Stephen Allwright

Renaming a table 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 table:

alter table old_name rename to new_name

Rename table in Snowflake if it 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 table. This is helpful as it stops potential errors being returned.

alter table if exists old_name rename to new_name

Renaming a table in Snowflake, things to watch out for

When renaming a table, there are two things to keep in mind:

  • The new table name must not be currently in use by another table in the same schema, but having the same table name across different schemas is fine
  • Renaming the table does not update references to that table in other objects (such as view definitions), so this must be updated to the new name manually

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:

Create table in Snowflake
Create or replace table in Snowflake
Swap Snowflake tables
Drop table in Snowflake
Delete table in Snowflake

References

Snowflake alter table documentation

Snowflake

Stephen Allwright Twitter

I'm a Data Scientist currently working for Oda, an online grocery retailer, in Oslo, Norway. These posts are my way of sharing some of the tips and tricks I've picked up along the way.

Comments