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 tableteam
, then create viewteamview
by selecting fromteam
. If you drop team withCASCADE
, the view remains in the database. If tableteam
is re-created, you can select fromteamview
based on the data inteam
.- 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