PostgreSQL DROP COLUMN

PostgreSQL DROP COLUMN (Beginner → Advanced)
In PostgreSQL DROP COLUMN is used to permanently remove a column from an existing table.
This operation deletes the column and all its data.
- Irreversible – once dropped, the column data cannot be recovered.
What is DROP COLUMN?
DROP COLUMNremoves a specific column from a table.
- Column removed
- Data removed
- Table remains intact
Basic DROP COLUMN Example
Table: students
| id | name | marks | age |
|---|
Drop age column
Resulting table:
| id | name | marks |
DROP COLUMN IF EXISTS (Safe Practice)
Avoids error if the column does not exist.
- Prevents runtime errors
- Recommended for scripts & migrations
Dropping Multiple Columns at Once
- Efficient
- Cleaner migrations
DROP COLUMN with CASCADE
Used when other objects depend on the column (indexes, views, constraints).
Removes:
dependent indexes
constraints
views using the column
Use carefully
DROP COLUMN with RESTRICT (Default)
- Prevents drop if dependencies exist
- Default behavior in PostgreSQL
DROP COLUMN and Transactions
- Column is NOT restored
DROP COLUMNis auto-commit- Cannot be rolled back
DROP COLUMN vs DELETE (Important Difference)
| Feature | DROP COLUMN | DELETE |
|---|---|---|
| Removes column | Yes | No |
| Removes rows | No | Yes |
| Removes data permanently | Yes | Conditional |
| Rollback | No | Yes |
- Use
DROP COLUMNfor schema changes - Use
DELETEfor data cleanup
Check Before Dropping a Column
- Always verify column existence
- Take backup if column is important
Real-Life Use Cases
Removing unused columns
Schema cleanup
Database refactoring
Removing deprecated fields
Optimizing table design
Common Mistakes
- Dropping column instead of deleting data
- Forgetting
IF EXISTS UsingCASCADEunintentionally- Running in production without backup
Interview Questions (Must Prepare)
How to drop a column in PostgreSQL?
Can
DROP COLUMNbe rolled back?Difference between
DROP COLUMNandDELETE?What does
CASCADEdo inDROP COLUMN?Can we drop multiple columns at once?
What happens to data in dropped column?
Summary
DROP COLUMNremoves a column permanently- Uses
ALTER TABLE IF EXISTSprevents errorsCASCADEremoves dependent objects- Cannot be rolled back
- Critical for schema design & interviews
