In this tutorial, you will understand how to use the DROP Table Statement to remove existing tables from the database in PostgreSQL.
PostgreSQL DROP table statement is used to define the
table and remove all the data, index, rule, trigger and obstacles related to
that table.
DROP table removes tables from the database. Only its
owner can demolish a table. To clear the table without any losses, use DELETE
or TRUNCATE.
The DROP table removes any indexes, rules, triggers,
and obstacles that are present for the target table. However, due to viewing
the foreign-key block of a table or any other table, CASCADE should be
specified. (CASCADE will completely remove a dependent approach, but in case of
foreign-key it will only remove foreign-key constraints, not completely other
tables.) You should be cautious while using this command because once the table is removed, all the available information in the table
will be permanently lost.
Parameters
IF EXISTS: If the table does not exist, do not enter an error. In
this case, a notice is issued.
Name: The name of the table to drop (optional schema-worthy).
CASCADE: Automatically reject any items that are dependent on the
table (such as views).
RESTRICT: If any object depends on it, then
refuse to leave the table. This is the default.
PostgreSQL DROP TABLE syntax
To delete a current table from the database, you use
the DROP table statement as shown below:
DROP
TABLE [IF EXIST] table_name[CASCADE/RESTRICT];
|
To permanently delete the table from the database, you
specify the name of the table after the DROP TABLE keyword.
If you delete a non-existent table, the PostgreSQL
problem is an inaccuracy. To avoid this situation, you can use the IF EXISTS
parameter after the DROP table clause.
If the table you want to delete is used in ideas,
constraints or any other object, then CASCADE gives you a grant to
automatically remove those dependent items with the table.
RESTRICT Refuse to drop the table if it has a dependent table
(foreign key relation). By default, PostgreSQL uses RESTRICT
After removing several tables of DROP tables together,
you can put a list of tables, each table is different from a comma.
Note that only the owner of the schema, superuser and
table owner has sufficient authority to remove the table.
PostgreSQL DROP TABLE examples
Suppose we have
a table Table1 and we don’t have it need onwards, so we have to remove this
table permanently from the database. Use the following syntax
If Table1 has the dependent table then above query will return the error:
[Err]
ERROR: cannot drop table Table1 because
other objects depend on it
DETAIL: constraint Table2_Table1_id_fkey on table Table2 depends on table Table1
HINT: Use DROP ... CASCADE to drop the dependent
objects too.
Use the following syntax if Table1 has a dependent table.
DROP
TABLE
Table1
CASCADE;
|
CASCADE removes the relations between tables.
If Table1 doesn’t exist then what will happen, absolutely
will raise Error:
[Err]
ERROR: Table Table1 doesn’t exist.
To resolve this issue, please use the following syntax.
DROP
TABLE IF EXISTS
Table1
CASCADE;
|