Skip to the workspace
Pebellslearn by building
Data Definition Language (DDL)  ›  Capstone Project

DROP TABLE

Drop Table 12 minDROP TABLE permanently removes a table's rows and structure, and dependent objects such as foreign keys either block it or must be dropped with it.

sql / DROP TABLE

Explore each scene at your own pace.

01 · The rule

The rule

DROP TABLE

DROP TABLE permanently removes a table's rows and structure, and dependent objects such as foreign keys either block it or must be dropped with it.

Inspect dependencies and the difference between removing rows and removing the table itself.

A shop ran a one-off promotion and created a promo_signups table for it. The promotion is over and the table is clutter. DROP TABLE removes it — not just the rows, but the table's very existence. It is the most final statement in this course, which is why it deserves its own short rule and a habit of pausing before you press Enter.

The rule

DROP removes the table, its rows and its shape — no undo.

DROP TABLE promo_signups;

After this there is no promo_signups table at all. Any query that mentions it fails with "table does not exist". Its columns, its data, its indexes and its constraints are gone together. Short of a backup (or, in PostgreSQL, a transaction you have not committed yet), nothing brings it back.

Two variations matter:

DROP TABLE IF EXISTS promo_signups;   -- no error if it is already gone
DROP TABLE customers CASCADE;         -- PostgreSQL: also drop things that depend on it

IF EXISTS makes cleanup scripts safe to re-run. CASCADE needs care. In PostgreSQL it drops dependent objects such as views built on the table, and removes foreign key constraints in other tables that point at it. It does not delete the orders table itself, but after it, orders no longer has any rule tying it to customers.

Without CASCADE, the database protects you, and that protection is a feature, not an obstacle: while orders has a foreign key pointing at customers, a plain DROP TABLE customers; is refused.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.