Deleting Columns from Tables in PostgreSQL (DROP COLUMN)
Syntax
ALTER TABLE table_name
DROP COLUMN column_name [CASCADE | RESTRICT];
Explanation:
table_name
: The table you want to modify.column_name
: The name of the column to delete.CASCADE
: Automatically drop dependent objects (use carefully).RESTRICT
: Prevent dropping if dependencies exist (default).
Suppose we have a table employees
with these columns:
Column | Data Type |
---|---|
id | SERIAL PRIMARY KEY |
name | VARCHAR(100) |
VARCHAR(100) | |
phone | VARCHAR(15) |
Example: Delete the phone
column
ALTER TABLE employees
DROP COLUMN phone;
After this command, the employees
table will look like this:
Column | Data Type |
---|---|
id | SERIAL PRIMARY KEY |
name | VARCHAR(100) |
VARCHAR(100) |
Note: The ALTER TABLE ... DROP COLUMN
command does not produce output.