August 2, 2023
Summary: This tutorial shows you how to use the PostgreSQL DROP COLUMN
clause in the ALTER TABLE
statement to remove one or more columns of a table.
Table of Contents
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:
ALTER TABLE table_name
DROP COLUMN column_name;
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, stored procedures, etc., you cannot drop the column because other objects are depending on it. In this case, you need to add the CASCADE
option to the DROP COLUMN
clause to drop the column and all of its dependent objects:
ALTER TABLE table_name
DROP COLUMN column_name CASCADE;
If you remove a column that does not exist, PostgreSQL will issue an error. To remove a column only if it exists, you can add the IF EXISTS
option as follows:
ALTER TABLE table_name
DROP COLUMN IF EXISTS column_name;
In this form, 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 in a single command, you use multiple DROP COLUMN
clause like this:
ALTER TABLE table_name
DROP COLUMN column_name1,
DROP COLUMN column_name2,
...;
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. The table has no column then. This is possible in PostgreSQL, but not possible according to SQL standard.
Let’s look at some examples to see how the ALTER TABLE DROP COLUMN
statement works.
PostgreSQL DROP COLUMN examples
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:
CREATE TABLE publishers (
publisher_id serial PRIMARY KEY,
name VARCHAR NOT NULL
);
CREATE TABLE categories (
category_id serial PRIMARY KEY,
name VARCHAR NOT NULL
);
CREATE TABLE books (
book_id serial PRIMARY KEY,
title VARCHAR NOT NULL,
isbn VARCHAR NOT NULL,
published_date DATE NOT NULL,
description VARCHAR,
category_id INT NOT NULL,
publisher_id INT NOT NULL,
FOREIGN KEY (publisher_id)
REFERENCES publishers (publisher_id),
FOREIGN KEY (category_id)
REFERENCES categories (category_id)
);
In addition, we create a view based on the books
and publishers
tables as follows:
CREATE VIEW book_info
AS SELECT
book_id,
title,
isbn,
published_date,
name
FROM
books b
INNER JOIN publishers
USING(publisher_id)
ORDER BY title;
The following statement uses the ALTER TABLE DROP COLUMN
statement to remove the category_id
column fro the books
table:
ALTER TABLE books
DROP COLUMN category_id;
Let’s show the structure of the books
table:
test=# \d books;
Table "public.books"
Column | Type | Modifiers
----------------+-------------------+---------------------------------------------------------
book_id | integer | not null default nextval('books_book_id_seq'::regclass)
title | character varying | not null
isbn | character varying | not null
published_date | date | not null
description | character varying |
publisher_id | integer | not null
Indexes:
"books_pkey" PRIMARY KEY, btree (book_id)
Foreign-key constraints:
"books_publisher_id_fkey" FOREIGN KEY (publisher_id) REFERENCES publishers(publisher_id)
As you can see from the output, the statement removed not only the category_id
column but also the foreign key constraint that involves the category_id
column.
The following statement attempts to remove the publisher_id
column:
ALTER TABLE books
DROP COLUMN publisher_id;
PostgreSQL issued the following error:
ERROR: cannot drop table books column publisher_id because other objects depend on it
DETAIL: view book_info depends on table books column publisher_id
HINT: Use DROP ... CASCADE to drop the dependent objects too.
It stated 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:
ALTER TABLE books
DROP COLUMN publisher_id CASCADE;
The statement issued the following notice, which is what we expected.
NOTICE: drop cascades to view book_info
To remove both isbn
and description
columns using a single statement, you add multiple DROP COLUMN
clauses as follows:
ALTER TABLE books
DROP COLUMN isbn,
DROP COLUMN description;
It worked as expected.
In this tutorial, you have learned how to use PostgreSQL DROP COLUMN
clause in the ALTER TABLE
statement to drop one or more columns from a table.