How Use The DROP TABLE Command in Snowflake

This is some text inside of a div block.
Published
May 2, 2024
Author

In this tutorial, we will be exploring the `DROP TABLE` command in Snowflake, its syntax, usage notes, and examples. This command is used to remove a table from the current or specified schema, with the possibility of recovery within a defined Time Travel retention period.

What Is the DROP TABLE Command?

The `DROP TABLE` command is used to remove a table from the current or specified schema in Snowflake. However, the table is not permanently deleted immediately. Instead, Snowflake retains a version of the table for a period defined by the Time Travel retention period, which allows for the possibility of recovery using the `UNDROP TABLE` command.

DROP TABLE [ IF EXISTS ] <table_name> [ CASCADE | RESTRICT ];

This command has several components: `IF EXISTS` is an optional clause that prevents an error from occurring if the table does not exist. `<table_name>` specifies the identifier for the table to drop. `CASCADE` drops the table even if there are foreign keys in other tables that reference it. `RESTRICT` prevents the table from being dropped if there are foreign keys in other tables that reference it.

How to Use the DROP TABLE Command

Before using the `DROP TABLE` command, it's important to understand that dropping a table does not permanently remove it from the system immediately. The table is retained in the Time Travel state for the duration specified by the Time Travel retention period for the table. After the Time Travel retention period, the next state for the dropped table depends on whether it is permanent, transient, or temporary.

DROP TABLE DEZYRE_TEST.PUBLIC.CUSTOMER;
DROP TABLE IF EXISTS DEZYRE_TEST.PUBLIC.CUSTOMER;

The first command drops a table named `CUSTOMER` in the schema `PUBLIC` of the database `DEZYRE_TEST`. The second command ensures that the command does not produce an error if the table does not exist.

Common Challenges and Solutions

There are a few challenges you might encounter when using the `DROP TABLE` command:

  • Remember that a long-running Time Travel query can delay the purging of tables.
  • Once a table has been purged, it cannot be recovered and must be recreated.
  • Before dropping a table, it is advisable to check that no views reference the table, as dropping a table that is referenced by a view will invalidate the view.

Best Practices

Here are some best practices to follow when using the `DROP TABLE` command:

  • Before dropping a table, ensure that you have a backup of your data if needed, as the operation cannot be reversed after the Time Travel retention period has expired.
  • To drop a table, you must have the necessary privileges on the table.
  • If a table is dropped and needs to be recovered within the Time Travel retention period, the `UNDROP TABLE` command can be used.

Further Learning

Here are some additional commands related to `DROP TABLE` that you might find useful:

  • `SHOW TABLES`: Lists the tables for which you have access privileges, including dropped tables that are still within the Time Travel retention period.
  • `DROP TABLE` statements can be generated programmatically for multiple tables using a query that selects from the `INFORMATION_SCHEMA.TABLES` view or by using a stored procedure.

Recap of DROP TABLE Command

In summary, the `DROP TABLE` command in Snowflake is a powerful feature that allows for the removal of tables while providing a safety net through the Time Travel and Fail-safe features for a limited time, enabling recovery of the dropped table if necessary. Remember to always have a backup of your data and ensure that you have the necessary privileges before dropping a table.

  • The `DROP TABLE` command removes a table from the current or specified schema.
  • The table is not permanently deleted immediately but is retained in the Time Travel state for a specified period.
  • The `UNDROP TABLE` command can be used to recover a dropped table within the Time Travel retention period.

Keep reading

See all