WikiGalaxy

Personalize

PostgreSQL Drop Column

Introduction to Dropping a Column

Dropping a column in PostgreSQL involves removing an existing column from a table. This operation is irreversible and should be done with caution, as it will permanently delete all data stored in that column.

Basic Syntax

Syntax:

The basic syntax for dropping a column is as follows:


ALTER TABLE table_name DROP COLUMN column_name;
      

Example 1: Dropping a Single Column

Scenario:

Let's say we have a table named "employees" with columns "id", "name", and "age". We want to drop the "age" column.


ALTER TABLE employees DROP COLUMN age;
      

This command will remove the "age" column from the "employees" table.

Example 2: Dropping Multiple Columns

Scenario:

If you need to drop multiple columns, you can do so in a single command. For instance, dropping "address" and "phone" from "customers".


ALTER TABLE customers DROP COLUMN address, DROP COLUMN phone;
      

This command removes both the "address" and "phone" columns from the "customers" table.

Example 3: Using CASCADE

Scenario:

When dropping a column that has dependencies, use the CASCADE option. For example, dropping "email" from "users" which has foreign key constraints.


ALTER TABLE users DROP COLUMN email CASCADE;
      

This command will drop the "email" column and all its dependencies.

Example 4: Using RESTRICT

Scenario:

To prevent accidental deletion, use the RESTRICT option. For example, dropping "salary" from "payroll" only if no dependencies exist.


ALTER TABLE payroll DROP COLUMN salary RESTRICT;
      

This command will fail if there are any dependencies on the "salary" column.

Example 5: Dropping a Column with Default Value

Scenario:

Dropping a column that has a default value set, for instance, "status" in "orders".


ALTER TABLE orders DROP COLUMN status;
      

This command removes the "status" column and its default value setting.

Example 6: Dropping a Column with Index

Scenario:

To drop a column that is indexed, such as "zipcode" in "locations".


ALTER TABLE locations DROP COLUMN zipcode;
      

This command will automatically remove the index on "zipcode".

Example 7: Dropping a Column with Check Constraint

Scenario:

Dropping a column that has a check constraint, like "rating" in "reviews".


ALTER TABLE reviews DROP COLUMN rating;
      

This command will remove the "rating" column along with its check constraint.

Example 8: Dropping a Column with Unique Constraint

Scenario:

Dropping a column with a unique constraint, such as "username" in "accounts".


ALTER TABLE accounts DROP COLUMN username;
      

This command will remove the "username" column and its unique constraint.

Example 9: Dropping a Column with Foreign Key

Scenario:

Dropping a column with a foreign key constraint, such as "author_id" in "books".


ALTER TABLE books DROP COLUMN author_id CASCADE;
      

This command will remove the "author_id" column along with its foreign key constraint.

Example 10: Dropping a Column with Dependent Views

Scenario:

Dropping a column that is used in a view, like "department" in "employees".


ALTER TABLE employees DROP COLUMN department CASCADE;
      

This command will drop the "department" column and any views that depend on it.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025