PostgreSQL DROP TABLE

Learn via video courses
Topics Covered

Overview

Do you know what happens to the data in a table when you use the DROP TABLE command? What is Postgresql?

PostgreSQL is an open-source relational database management system (RDBMS) that provides a robust and feature-rich platform for storing, managing, and retrieving structured data. It offers a wide range of advanced functionalities and is renowned for its scalability, stability, and adherence to SQL standards. It supports ACID (Atomicity, Consistency, Isolation, Durability) properties, ensuring that transactions are processed reliably and consistently.

When Postgres drops all tables, it permanently removes the specified table and all the associated data from the database. The data contained within the table, including all rows and columns, is irreversibly deleted.

Before dropping a table, it is essential to consider that data stored in the table can get lost. Therefore, it is necessary to take appropriate backups or ensure a copy of the data if it is to be withheld.

Introduction to PostgreSQL DROP TABLE Command

The DROP TABLE in Postgresql is used to remove a table and its associated data from the database. It allows you to delete an entire table, including all its columns, indexes, constraints, and triggers.

When Postgres drops all tables, it permanently deletes the specified table and all its data from the database. This action is not reversible, so it is necessary to use this command when it is needed. It is recommended to take appropriate backups or ensure that you have a copy of the data before proceeding.

PostgreSQL also provides additional options to customize the behavior of the DROP TABLE command. For instance, you can use the CASCADE keyword to automatically drop any objects that depend on the table, such as views or foreign key constraints. It ensures that all related objects are removed along with the table.

It is important to note that dropping a table using the command DROP TABLE in Postgresql also removes any associated indexes, triggers, and constraints. However, the table's schema and other objects that depend on the table, such as stored procedures or functions, remain unaffected.

Before executing the DROP TABLE command, it is recommended to double-check that you are targeting the correct table and that you have backups or copies of the data if needed. Careful consideration should be given to any dependent objects or data dependencies that might be affected.

Syntax

To drop a table in PostgreSQL, you can use the following syntax:

Let's break down the components of the syntax:

  • DROP TABLE in Postgresql is the statement used to indicate that you want to drop a table.
  • IF EXISTS is an optional clause that allows you to avoid an error if the table doesn't exist. If the table does not exist, the command will simply be ignored.
  • Multiple tables can be dropped simultaneously by separating their names with commas.
  • The optional CASCADE keyword allows you to automatically drop any dependent objects, such as views or foreign key constraints, along with the table. This ensures that all related objects are removed.
  • The optional RESTRICT keyword specifies that the table can only be dropped if no other objects depend on it. If any dependent objects exist, the command will fail.

Parameters

Below are the parameters used in the DROP TABLE in Postgresql:

  • IF EXISTS:
    This optional parameter allows you to avoid an error if the table does not exist. If you include the IF EXISTS clause, the command will be silently ignored if the table is not found.
  • table_name:
    This parameter specifies the name of the table you want to drop. You can provide a single table name or multiple table names separated by commas.
  • CASCADE:
    The CASCADE keyword, when specified, automatically drops all objects that depend on the table being dropped. This includes views, foreign key constraints, and other dependent objects. Dropping a table with CASCADE ensures that all associated objects are removed.
  • RESTRICT:
    The RESTRICT keyword, if used, restricts the drop operation only if there are dependent objects. If any objects depend on the table, the DROP TABLE command will fail and an error will be raised.

Examples

Let’s consider we have a database named employees and orders. Now we will create both databases:

Now we will implement the DROP command on the above database.

Drop a Table from the Database

To drop a table named employees from the database:

Syntax:

Here is the Syntax for dropping the table from the database.

In this case, we are dropping the employees table.

Below we have 4 tables in which we will drop the employee's table.

drop-a-table-from-the-database-in-power-bi

Output:

The below image shows the execution of the above command which confirms that the table is completely deleted from the database.

output-of-drop-a-table-from-the-database-in-power-bi

This command will permanently remove the employees table and its associated data from the database.

Drop a Table that has Dependent Objects

Suppose you have a table named orders that has a foreign key constraint referencing the customers table. To drop the orders table and its dependent objects:

Syntax:

Here is the syntax for dropping the table from the database which has dependent objects.

The structure of the orders and customers tables has been shown below:

drop-a-table-that-has-dependent-objects-in-power-bi

Here, as you can see the table orders have a foreign key as orders_customer_id which is dependent on the table customers.

drop-a-table-that-has-dependent-objects-in-power-bi-1

Now, we can drop the table orders which is dependent on the table customers with the help of the CASCADE keyword.

Output:

The CASCADE keyword ensures that the foreign key constraint is also dropped along with the orders table.

Drop Multiple Tables

If you want to drop multiple tables simultaneously, you can specify their names separated by commas. For example, to drop tables named table1, table2, and table3:

drop-multiple-tables-in-power-bi

Now, let's execute the multiple drop table command.

Output:

output-of-drop-multiple-tables-in-power-bi

The above output shows that all three tables are dropped from the database.

Conclusion

  • PostgreSQL is an open-source relational database management system (RDBMS) that provides a robust and feature-rich platform for storing, managing, and retrieving structured data.
  • When the Postgres drop all tables, it removes a table and its associated data from the database.
  • It allows you to permanently delete a table, including its columns, indexes, constraints, and triggers.
  • The optional IF EXISTS clause avoids errors if the table does not exist, preventing the command from raising an error.
  • Double-check the table name and consider any dependent objects or data dependencies before executing the command.
  • Backing up or having copies of the data is recommended to ensure data preservation.