DROP COLUMN

The DROP COLUMN statement is part of ALTER TABLE and removes columns from a table.

Tip:

New in v19.1: This command can be combined with other ALTER TABLE commands in a single statement. For a list of commands that can be combined, see ALTER TABLE. For a demonstration, see Add and rename columns atomically.

Synopsis

ALTERTABLEIFEXISTStable_nameDROPCOLUMNIFEXISTSnameCASCADERESTRICT

Required privileges

The user must have the CREATE privilege on the table.

Parameters

ParameterDescription
tablenameThe name of the table with the column you want to drop.
nameThe name of the column you want to drop.When a column with a CHECK constraint is dropped, the CHECK constraint is also dropped.
CASCADEDrop 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.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.

Viewing schema changes

This schema change statement is registered as a job. You can view long-running jobs with SHOW JOBS.

Examples

Drop columns

If you no longer want a column in a table, you can drop it.

  1. > ALTER TABLE orders DROP COLUMN billing_zip;

Prevent dropping columns with dependent objects (RESTRICT)

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.

  1. > ALTER TABLE orders DROP COLUMN customer RESTRICT;
  1. pq: cannot drop column "customer" because view "customer_view" depends on it

Drop column and dependent objects (CASCADE)

If you want to drop the column and all of its dependent options, include the CASCADE clause.

Warning:
CASCADE does not list objects it drops, so should be used cautiously.

  1. > SHOW CREATE customer_view;
  1. +---------------+----------------------------------------------------------------+
  2. | table_name | create_statement |
  3. +---------------+----------------------------------------------------------------+
  4. | customer_view | CREATE VIEW customer_view AS SELECT customer FROM store.orders |
  5. +---------------+----------------------------------------------------------------+
  1. > ALTER TABLE orders DROP COLUMN customer CASCADE;
  1. > SHOW CREATE customer_view;
  1. pq: view "customer_view" does not exist

See also

Was this page helpful?
YesNo