ALTER TABLE DROP COLUMN
Removes a column and the data in it. All five engines can do it in one statement today; what differs is how hard they fight you when other objects still use the column.
- Categoria
- Colunas
- In this category
- 3 of 5
Sintaxe
ALTER TABLE t DROP COLUMN col -- SQL Server, PostgreSQL, MySQL, SQLite
ALTER TABLE t DROP COLUMN col CASCADE -- PostgreSQL: drops dependent objects too
ALTER TABLE t DROP (col) -- Oracle
ALTER TABLE t SET UNUSED (col) -- Oracle: hide now, drop later
Each block is a separate form. The support table and the examples show which engine takes which.
Suporte a mecanismos
| Motor | Support | Notas |
|---|---|---|
| SQL Server | Sim | DROP COLUMN c1, c2; refuses columns used by indexes, constraints or computed columns |
| PostgreSQL | Sim | IF EXISTS, and RESTRICT (default) or CASCADE |
| MySQL | Sim | DROP COLUMN, several in one ALTER TABLE |
| SQLite | Different syntax | 3.35+; refuses PRIMARY KEY, UNIQUE and indexed columns |
| Oracle | Different syntax | DROP (col), or SET UNUSED and DROP UNUSED COLUMNS later |
Exemplos
PostgreSQL
ALTER TABLE orders DROP COLUMN legacy_ref;
PostgreSQL
ALTER TABLE orders DROP COLUMN IF EXISTS legacy_ref CASCADE;
SQL Server
ALTER TABLE dbo.orders DROP COLUMN legacy_ref;
MySQL
ALTER TABLE orders DROP COLUMN legacy_ref, DROP COLUMN temp_flag;
SQLite
-- needs SQLite 3.35+, and no index on the column ALTER TABLE orders DROP COLUMN legacy_ref;
Oracle
ALTER TABLE orders SET UNUSED (legacy_ref); ALTER TABLE orders DROP UNUSED COLUMNS;
Bom saber
- PostgreSQL's default is RESTRICT: the drop fails while a view, or a foreign key in another table, depends on the column. Indexes and constraints on the table itself are dropped along with the column without asking. CASCADE removes the outside dependents silently too, so read the dependency list (or drop them yourself) before reaching for it.
- SQLite allows DROP COLUMN only from 3.35, and refuses columns that are PRIMARY KEY or UNIQUE, appear in an index, or are referenced by a trigger or view. Anything older rebuilds the table.
- Oracle's SET UNUSED marks columns invisible immediately without paying to remove them; DROP UNUSED COLUMNS reclaims the space later, in a quiet window. A SET UNUSED column cannot be read again, it is only hidden.
- SQL Server refuses when the column is in an index or constraint: drop those first. Views and procedures that selected the column are not updated, and they fail the next time they run.
Questions about DROP COLUMN
Does DROP COLUMN reclaim the disk space right away?
Not always. PostgreSQL and SQL Server drop the column as metadata and leave the values in place until rows are rewritten (SQL Server ships DBCC CLEANTABLE for exactly that). Oracle's SET UNUSED plus DROP UNUSED COLUMNS is the scheduled version of the same idea.
Why can I not drop this column?
Something depends on it: an index, a primary or unique key, a check or foreign key constraint, a view or a trigger. SQL Server and SQLite refuse and name the offender in the error, so remove it first (SQLite lets a column's own CHECK go with it). PostgreSQL drops the table's own indexes and constraints along with the column and refuses only for views and other tables' foreign keys, which CASCADE removes for you. MySQL trims indexes and refuses only when a foreign key needs the column.
How do I drop several columns at once?
SQL Server: DROP COLUMN a, b. MySQL: DROP COLUMN a, DROP COLUMN b in one ALTER TABLE. PostgreSQL: DROP COLUMN a, DROP COLUMN b in one statement. Oracle: DROP (a, b). SQLite: one column per statement.
Browse every statement in the SQL DDL reference, or see what the SQLly desktop app does for your database.
DDL that matches the engine you're connected to
SQLly checks your statements against the dialect of the open connection, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle, so an ALTER TABLE that works on one engine gets flagged before it runs on another.