Skip to content

DROP TABLE

Drop a table from the database.

DROP TABLE [ IF EXISTS ] name [, ...] [ CASCADE | RESTRICT ]
IF EXISTS

Do not return a warning if the table does not exist.

name

Name the table to drop, optionally qualified by its schema name. You can drop multiple tables with one statement.

CASCADE

If other objects depend on the table, drop them as well.

Views are not treated as dependent on tables; therefore, they persist in the database when their tables are dropped with the CASCADE option. For example, you can create table team, then create view teamview by selecting from team. If you drop team with CASCADE, the view remains in the database. If table team is re-created, you can select from teamview based on the data in team.

RESTRICT

If other objects depend on the table, do not drop the table. This is the default; however, views are not dependent objects.

For example, drop three tables with one statement:

premdb=# drop table awayteam, hometeam, team;
DROP TABLE

Create a view, then drop the table that the view selects from:

premdb=# create view htv as select * from hometeam;
CREATE VIEW
premdb=# drop table hometeam restrict;
DROP TABLE

Note that the RESTRICT option does not prevent the table from being dropped. The view is considered independent and persists until it is dropped explicitly with a DROP VIEW statement.

Parent topic:SQL Commands