postgresql
  1. postgresql-drop-table

DROP TABLE - (PostgreSQL Table)

The DROP TABLE statement in PostgreSQL is used to delete a table and all of its associated objects, such as indexes, constraints, and triggers. In this tutorial, we'll look at the syntax, example, output, explanation, use, important points, and summary of DROP TABLE statement in PostgreSQL.

Syntax

The basic syntax of DROP TABLE statement in PostgreSQL is as follows:

DROP TABLE [IF EXISTS] table_name [CASCADE | RESTRICT];
  • IF EXISTS (optional): Prevents an error from occurring when attempting to drop a non-existent table.
  • table_name: The name of the table to be dropped.
  • CASCADE / RESTRICT (optional): Used to define the behavior of dependent objects when attempting to drop a table.

Example

Let's take a look at an example of the DROP TABLE statement in PostgreSQL:

DROP TABLE employees;

In this example, the DROP TABLE statement is used to delete the "employees" table from the database.

Output

The output of the above DROP TABLE statement will be as follows:

DROP TABLE

Explanation

In the above example, the DROP TABLE statement is used to delete the "employees" table from the database. When the statement executes successfully, it returns a message confirming the deletion of the table without any further information.

Use

The DROP TABLE statement is used to remove a table from the database. This is useful when you no longer need a table, or if the table was created during testing and is no longer needed in production.

Important Points

  • The DROP TABLE statement permanently removes the table and its associated objects from the database.
  • The CASCADE keyword can be included to also drop all objects that depend on the table, such as indexes, constraints, and triggers.
  • The RESTRICT keyword can be used to prevent the dropping of a table if any objects depend on it.
  • Dropping a table cannot be undone, so be careful when using this statement.

Summary

In this tutorial, we discussed the DROP TABLE statement in PostgreSQL. We covered the syntax, example, output, explanation, use, and important points of the DROP TABLE statement. With this knowledge, you can now use the DROP TABLE statement to delete unwanted tables from your PostgreSQL database.

Published on: