DROP CONSTRAINT

2020-02-16

The DROP CONSTRAINT statement is part of ALTER TABLE and removes Check and Foreign Key constraints from columns.

{{site.data.alerts.callout_info}}For information about removing other constraints, see Constraints: Remove Constraints.{{site.data.alerts.end}}

Synopsis

{% include {{ page.version.version }}/sql/diagrams/drop_constraint.html %}

Required privileges

The user must have the CREATE privilege on the table.

Parameters

Parameter Description
table_name The name of the table with the constraint you want to drop.
name The name of the constraint you want to drop.

Viewing schema changes

Whenever you initiate a schema change, CockroachDB registers it as a job, which you can view with SHOW JOBS.

Example

> SHOW CONSTRAINTS FROM orders;
+--------+---------------------------+-------------+-----------+----------------+
| Table  |           Name            |    Type     | Column(s) |    Details     |
+--------+---------------------------+-------------+-----------+----------------+
| orders | fk_customer_ref_customers | FOREIGN KEY | customer  | customers.[id] |
| orders | primary                   | PRIMARY KEY | id        | NULL           |
+--------+---------------------------+-------------+-----------+----------------+
> ALTER TABLE orders DROP CONSTRAINT fk_customer_ref_customers;
ALTER TABLE
> SHOW CONSTRAINTS FROM orders;
+--------+---------+-------------+-----------+---------+
| Table  |  Name   |    Type     | Column(s) | Details |
+--------+---------+-------------+-----------+---------+
| orders | primary | PRIMARY KEY | id        | NULL    |
+--------+---------+-------------+-----------+---------+

{{site.data.alerts.callout_info}}You cannot drop the primary constraint, which indicates your table's Primary Key.{{site.data.alerts.end}}

See also