title | summary | toc |
---|---|---|
DROP COLUMN |
Use the ALTER COLUMN statement to remove columns from tables. |
true |
The DROP COLUMN
statement is part of ALTER TABLE
and removes columns from a table.
{% include {{ page.version.version }}/sql/diagrams/drop_column.html %}
The user must have the CREATE
privilege on the table.
Parameter | Description |
---|---|
table_name |
The name of the table with the column you want to drop. |
name |
The name of the column you want to drop. When a column with a CHECK constraint is dropped, the CHECK constraint is also dropped. |
CASCADE |
Drop the column even if objects (such as views) depend on it; drop the dependent objects, as well.CASCADE does not list objects it drops, so should be used cautiously. However, CASCADE will not drop dependent indexes; you must use DROP INDEX .New in v2.0: CASCADE will drop a column with a foreign key constraint if it is the only column in the reference. |
RESTRICT |
(Default) Do not drop the column if any objects (such as views) depend on it. |
{% include {{ page.version.version }}/misc/schema-change-view-job.md %}
If you no longer want a column in a table, you can drop it.
> ALTER TABLE orders DROP COLUMN billing_zip;
If the column has dependent objects, such as views, CockroachDB will not drop the column by default; however, if you want to be sure of the behavior you can include the RESTRICT
clause.
> ALTER TABLE orders DROP COLUMN customer RESTRICT;
pq: cannot drop column "customer" because view "customer_view" depends on it
If you want to drop the column and all of its dependent options, include the CASCADE
clause.
{{site.data.alerts.callout_danger}}CASCADE
does not list objects it drops, so should be used cautiously.{{site.data.alerts.end}}
> SHOW CREATE VIEW customer_view;
+---------------+----------------------------------------------------------------+
| View | CreateView |
+---------------+----------------------------------------------------------------+
| customer_view | CREATE VIEW customer_view AS SELECT customer FROM store.orders |
+---------------+----------------------------------------------------------------+
> ALTER TABLE orders DROP COLUMN customer CASCADE;
> SHOW CREATE VIEW customer_view;
pq: view "customer_view" does not exist