Learn How to Drop the Table in PostgreSQL

truncate table in postgresql
Image by Markus Spiske from Pixabay

In this tutorial, we are going to see how to drop the table in postgreSQL. It is used to remove the table permanently from the database.

Here is the syntax of postgresql drop table command:

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

In the above command, table_name is the name of table which we want to remove from the database. [IF EXISTS] is an optional clause which is used to prevent from getting an error if object does not exist. It will show the notice instead.

If you want to remove the depending objects of the table being deleted, you can specify CASCADE option. It will delete all the depending objects like views, constraints or any other objects etc.

RESTRICT is used to specify that do not delete the table if there are any dependent. By default, postgreSQL uses RESTRICT option.

You can specify a list of tables separated by comma in case you want to delete multiple tables.

Let’s see its example:

DROP TABLE IF EXISTS students,teachers;

Above command will drop the tables if they exist in the database.

Leave a Reply

Your email address will not be published. Required fields are marked *