PostgreSQL DROP COLUMN: Remove One or More Columns of a Table
Summary: In this tutorial, you will learn how to use the PostgreSQL DROP COLUMN
clause in the ALTER TABLE
statement to remove one or more columns from a table.
Introduction to PostgreSQL DROP COLUMN clause
To drop a column of a table, you use the DROP COLUMN
clause in the ALTER TABLE
statement as follows:
When you remove a column from a table, PostgreSQL will automatically remove all of the indexes and constraints that involved the dropped column.
If the column that you want to remove is used in other database objects such as views, triggers, and stored procedures, you cannot drop the column because other objects depend on it.
In this case, you can use the CASCADE
option in the DROP COLUMN
clause to drop the column and all of its dependent objects:
If you remove a column that does not exist, PostgreSQL will issue an error. To remove a column if it exists only, you can use the IF EXISTS
option as follows:
In this syntax, if you remove a column that does not exist, PostgreSQL will issue a notice instead of an error.
If you want to drop multiple columns of a table simultaneously, you use multiple DROP COLUMN
clauses in the ALTER TABLE
statement like this:
Notice that you need to add a comma (,
) after each DROP COLUMN
clause.
If a table has one column, you can use drop it using the ALTER TABLE...DROP COLUMN
statement. Consequently, the table will have no columns.
It’s worth noting that while PostgreSQL allows a table that has no column, it may be not allowed according to the standard SQL.
PostgreSQL DROP COLUMN examples
Let’s look at some examples to see how the ALTER TABLE...DROP COLUMN
statement works.
We will create three tables: books
, categories
, and publishers
for the demonstration.
In this diagram, each book has only one publisher and each publisher can publish many books. Each book is assigned to a category and each category can have many books.
The following statements create the three tables:
Additionally, we create a view based on the books
and publishers
tables as follows:
1) Drop a column example
First, drop the category_id
column in the books
table using the ALTER TABLE...DROP COLUMN
statement:
Second, view the structure of the books
table in psql:
Output:
The output indicates that the statement removes both the category_id
column and the foreign key constraint that involves the category_id
column.
2) Drop a column that is referenced by a constraint
First, attempt to remove the publisher_id
column from the books
table:
PostgreSQL issued the following error:
The output states that the book_info
view is using the column publisher_id
of the books
table. You need to use the CASCADE
option to remove both the publisher_id
column and book_info
view as shown in the following statement:
The statement issued the following notice indicating that the view book_info was also removed:
3) Drop multiple columns example
To remove both isbn
and description
columns simultaneously, you can use multiple DROP COLUMN
clauses as follows:
When viewing the books table, you’ll see that those columns were removed:
Output:
Summary
- Use the PostgreSQL
ALTER TABLE ... DROP COLUMN
statement to drop one or more columns from a table.