PostgreSQL DROP TABLE
DROP TABLE removes a table completely: its rows, its columns, its indexes and its constraints. Compare it with the statements you already know:
| Statement | Rows | Table structure |
|---|---|---|
DELETE FROM t |
removed (all or some) | kept |
TRUNCATE t |
all removed | kept |
DROP TABLE t |
removed | removed |
Dropping a table
CREATE TABLE temp_import (line text);
INSERT INTO temp_import VALUES ('row one'), ('row two');
DROP TABLE temp_import;
SELECT * FROM temp_import;CREATE TABLE
INSERT 0 2
DROP TABLE
ERROR: relation "temp_import" does not exist
LINE 1: SELECT * FROM temp_import;
^
Once dropped, the table is gone; the following SELECT fails. On a real server there is no recycle bin, so only a backup (or a transaction you haven't committed yet) can bring it back.
IF EXISTS
Dropping a table that doesn't exist is an error. IF EXISTS turns it into a notice, which makes clean-up scripts safe to run more than once:
DROP TABLE wishlist;
DROP TABLE IF EXISTS wishlist;ERROR: table "wishlist" does not exist
NOTICE: table "wishlist" does not exist, skipping
DROP TABLE
Dependencies and CASCADE
PostgreSQL tracks which objects depend on each other. books is referenced by the foreign key in order_items, so it cannot simply be dropped:
DROP TABLE books;ERROR: cannot drop table books because other objects depend on it
DETAIL: constraint order_items_book_id_fkey on table order_items depends on table books
HINT: Use DROP ... CASCADE to drop the dependent objects too.
The DETAIL lists what depends on the table and the HINT suggests CASCADE. CASCADE drops the dependent constraints and views, not the other tables themselves:
CREATE VIEW cheap_titles AS SELECT title FROM books WHERE price < 10;
DROP TABLE books CASCADE;
SELECT table_name FROM information_schema.tables
WHERE table_schema = 'public' ORDER BY table_name;
SELECT count(*) AS order_items_still_there FROM order_items;CREATE VIEW
NOTICE: drop cascades to 2 other objects
DETAIL: drop cascades to constraint order_items_book_id_fkey on table order_items
drop cascades to view cheap_titles
DROP TABLE
table_name
-------------
authors
categories
customers
employees
order_items
orders
(6 rows)
order_items_still_there
-------------------------
34
(1 row)
The view cheap_titles vanished with the table, and order_items lost its foreign key to books but kept all of its rows.
Tip: Read the
NOTICElines thatCASCADEprints. They list everything that was removed along with the table.
Dropping several tables
List several tables in one statement. Order doesn't matter when they reference each other, because they are dropped together:
DROP TABLE order_items, orders;
SELECT count(*) AS tables_left
FROM information_schema.tables
WHERE table_schema = 'public';DROP TABLE
tables_left
-------------
5
(1 row)
Test yourself
Which statement removes both the rows and the table definition?
- DELETE FROM t
- TRUNCATE t
- DROP TABLE t
- ALTER TABLE t DROP ROWS
Why does DROP TABLE books; fail in the sample database?
- books is too large
- Other objects (the order_items foreign key) depend on it
- Tables can't be dropped in PostgreSQL
- You need IF EXISTS
What does DROP TABLE books CASCADE do to order_items?
- Drops it too
- Deletes its rows
- Removes its foreign key constraint but keeps the table and rows
- Nothing