Sub topic 9
Removing a Table with `DROP TABLE`
Assume you know basic SELECT, INSERT, UPDATE, and DELETE statements, can create tables, and understand primary and foreign keys. This chapter focuses on removing a table and predicting what happens to objects that depend on it.
What `DROP TABLE` Does
DROP TABLE removes a table from the database. It removes both:
- The table definition, including columns and constraints.
- All rows stored in the table.
The operation is different from deleting rows. After a successful drop, queries cannot use the table unless you create it again.
DROP TABLE orders;This statement fails if orders does not exist. It can also fail when another object depends on the table, such as a foreign key or a view.
A safer form is IF EXISTS:
DROP TABLE IF EXISTS orders;With this form, the database does nothing if orders is already absent. This makes setup and cleanup scripts safe to run more than once. Support and exact behavior can vary by database system, so check the dialect you use.
Dependencies and Foreign Keys
Suppose orders.customer_id references customers.id. The orders table depends on customers, because its foreign key needs the referenced table to exist.
flowchart TD
C[customers table] -->|referenced by foreign key| O[orders table]
C -->|used by view| V[customer_summary view]A database usually refuses this operation:
DROP TABLE customers;Refusing the operation protects dependent objects from becoming invalid. This behavior is often called RESTRICT, either explicitly or by default.
DROP TABLE customers RESTRICT;The CASCADE option tells the database to remove dependent objects that it can remove safely:
DROP TABLE customers CASCADE;In PostgreSQL, this drops dependent foreign key constraints and dependent views. It does not automatically drop the orders table merely because orders contains the foreign key. The table remains, but its foreign key constraint is removed.
Use CASCADE only after identifying every affected object. A broad cascade can remove views, constraints, and other schema objects that an application still needs.
Compare Destructive Table Operations
| Statement | Removes rows | Removes table definition | Supports a filter |
|---|---|---|---|
| `DELETE` | Yes | No | Yes, with `WHERE` |
| `TRUNCATE` | Yes, usually all | No | No |
| `DROP TABLE` | Yes, all | Yes | No |
DROP TABLE is usually the least reversible choice. Before running it, confirm the table name, inspect dependencies, and take a backup or snapshot if the data may be needed later.
A Safe Removal Process
- Check that the target is the intended table.
- Find foreign keys, views, procedures, and jobs that reference it.
- Decide whether dependent objects should block the operation or be removed.
- Test the command in a nonproduction database.
- Use a migration script that records the change.
- Run the command with the database account that has the required permission.
Some database systems support transactional DDL, allowing a drop to be rolled back inside a transaction. Others commit schema changes immediately. Do not assume a rollback will work across database products.
Exercise
The orders table has a foreign key that references customers, and a view also reads from customers. What should you check before using DROP TABLE customers CASCADE?
Show answer
Check that removing the foreign key and view will not break the application, and keep a backup or tested recreation script before running the command.