PostgreSQL DROP TABLE

PostgreSQL DROP TABLE (Beginner → Advanced)
In PostgreSQL DROP TABLE is used to permanently delete a table from the database, including:
all data (rows)
table structure
indexes
constraints
triggers
This action is irreversible.
What is DROP TABLE?
DROP TABLEremoves a table completely from the database.
- Table is deleted
- Data is deleted
- Structure is deleted
Basic DROP TABLE Example
After execution:
studentstable no longer existsCannot run
SELECT * FROM students
DROP TABLE IF EXISTS (Safe Practice)
Avoids error if the table does not exist.
- Prevents runtime errors
- Recommended in scripts
Dropping Multiple Tables at Once
- Deletes multiple tables in one command
DROP TABLE with CASCADE
Used when other objects depend on the table (foreign keys, views).
Drops:
dependent foreign key constraints
views referencing the table
Dangerous if used blindly
DROP TABLE with RESTRICT (Default)
- Prevents drop if dependencies exist
- Default behavior in PostgreSQL
DROP TABLE vs DELETE vs TRUNCATE (Very Important)
| Feature | DROP TABLE | TRUNCATE | DELETE |
|---|---|---|---|
| Removes table structure | Yes | No | No |
| Removes all data | Yes | Yes | Yes |
| WHERE clause | No | No | Yes |
| Rollback possible | No | Limited | Yes |
| Speed | Fastest | Very Fast | Slower |
- Use
DROP TABLEwhen table is no longer needed
Transaction Behavior
- Table is NOT restore
DROP TABLEis auto-commit in PostgreSQL- Cannot be rollback
Check Before Dropping a Table
- Always verify data
- Take backup if needed
Real-Life Use Cases
Removing temporary tables
Cleaning old/unused tables
Resetting development databases
Database migration scripts
Common Mistakes
- Dropping table instead of deleting rows
- Forgetting
IF EXISTS - Using CASCADE unintentionally
- Running in production without backup
Interview Questions (Must Prepare)
What does
DROP TABLEdo in PostgreSQL?Difference between
DROP,TRUNCATE, andDELETEWhat is
CASCADEinDROP TABLE?Can
DROP TABLEbe rolled back?What happens to dependent objects?
Summary
- DROP TABLE permanently deletes a table
- Removes data + structure
- IF EXISTS prevents errors
- CASCADE removes dependent objects
- Cannot be rollback
- Use carefully, especially in production
