Deleting Columns from Tables in PostgreSQL (DROP COLUMN)

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:

ColumnData Type
idSERIAL PRIMARY KEY
nameVARCHAR(100)
emailVARCHAR(100)
phoneVARCHAR(15)

Example: Delete the phone column

ALTER TABLE employees
DROP COLUMN phone;

After this command, the employees table will look like this:

ColumnData Type
idSERIAL PRIMARY KEY
nameVARCHAR(100)
emailVARCHAR(100)

Note: The ALTER TABLE ... DROP COLUMN command does not produce output.


Reference: Official PostgreSQL Documentation: ALTER TABLE

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top