DROP TABLE removes a table: its columns, its rows, its constraints. The tab of the table goes too.
DROP TABLE tmp_import;tmp_import
| id | raw_line |
|---|---|
| 1 | Kraken Plush;29.99 |
| 2 | Root Hoodie;49.95 |
There is no undo. PostgreSQL does not ask "are you sure?". Read the table name again before you run the statement. To remove some rows and keep the table, you use DELETE (Chapter 6).
A table that does not exist
ERROR: table "tmp_import" does not existIF EXISTS skips the statement when the table is missing:
DROP TABLE IF EXISTS tmp_import;NOTICE: table "tmp_import" does not exist, skippingSeveral tables
Separate the names with commas:
DROP TABLE IF EXISTS products_old, products_backup;Build a table again
A script that must work on every run often starts with a DROP:
DROP TABLE IF EXISTS price_watch;
CREATE TABLE price_watch (
product_id integer,
target_price numeric(8,2)
);The first run finds no table, and the DROP only shows a NOTICE. Each later run removes the old table and its rows, then creates it again. Use this only for tables whose rows you do not need.