Before continuing: This entry is part of the ALTER TABLE guide. If you are new to the topic, start with ALTER TABLE.
Query
CREATE TABLE products_demo (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
internal_note TEXT
);
INSERT INTO products_demo (id, name, internal_note)
VALUES (1, 'Mouse', 'check packaging');
ALTER TABLE products_demo
DROP COLUMN internal_note;
SELECT *
FROM products_demo;The value “check packaging” disappears along with the column. To get it back you would need a copy, or a reverse migration that keeps that data somewhere else.
Cases that change the result
Checks beforehand
- Search applications, queries and reports for reads and writes of the column name.
- Review keys, indexes, constraints, views and triggers.
- Decide whether the data should be archived before it is deleted.
- Measure the size of the table and any locking it might cause.
- Prepare a backup and a rollback plan.
What happens to dependencies
PostgreSQL distinguishes between RESTRICT, which rejects the change if dependent objects exist, and CASCADE, which also removes the dependencies. Using CASCADE without checking what will disappear can make the operation far bigger than you meant.
SQLite rejects DROP COLUMN when the column is part of a primary key, has a UNIQUEconstraint, is indexed or appears in various kinds of schema objects. MySQL and SQL Server also have their own rules and costs.
Avoid this confusion
Dropping it before you stop using it
A safer strategy splits the change into stages: stop writing to the column, migrate or archive the data, deploy applications that no longer read it, and drop it in a later release.
Do not use SELECT * in applications that rely on column order. A schema change can alter the shape of the result even when the code never mentions the dropped column.