Contents
DROP TABLE
An SQL command for removing a table
DROP TABLE
is a DDL command for removing a table.
DROP TABLE
has always been present in PostgreSQL.
Change history
- PostgreSQL 8.2
DROP TABLE IF EXISTS ...
syntax added (commit daea4d8e)
- PostgreSQL 7.3
CASCADE
andRESTRICT
clauses added (commit 131f801d)
Examples
Basic execution example for DROP TABLE
:
postgres=# DROP TABLE foo; DROP TABLE postgres=# DROP TABLE foo, bar; DROP TABLE
Safely attempting to drop a table which might not exist:
postgres=# DROP TABLE if exists foo; NOTICE: table "foo" does not exist, skipping DROP TABLE
Dropping a table with dependencies:
postgres=# DROP TABLE bar; ERROR: cannot drop table bar because other objects depend on it DETAIL: constraint baz_id_fkey on table baz depends on table bar HINT: Use DROP ... CASCADE to drop the dependent objects too. postgres=# DROP TABLE bar CASCADE; NOTICE: drop cascades to constraint baz_id_fkey on table baz DROP TABLE
References
- PostgreSQL documentation: DROP TABLE