/v1.1/drop-constraint.md

https://github.com/cockroachdb/docs · Markdown · 66 lines · 51 code · 15 blank · 0 comment · 0 complexity · fc69525ab634b1cc9f9665a9f527d941 MD5 · raw file

  1. ---
  2. title: DROP CONSTRAINT
  3. summary: Use the ALTER CONSTRAINT statement to remove constraints from columns.
  4. toc: true
  5. ---
  6. The `DROP CONSTRAINT` [statement](sql-statements.html) is part of `ALTER TABLE` and removes Check and Foreign Key constraints from columns.
  7. {{site.data.alerts.callout_info}}For information about removing other constraints, see <a href="constraints.html#remove-constraints">Constraints: Remove Constraints</a>.{{site.data.alerts.end}}
  8. ## Synopsis
  9. {% include {{ page.version.version }}/sql/diagrams/drop_constraint.html %}
  10. ## Required Privileges
  11. The user must have the `CREATE` [privilege](privileges.html) on the table.
  12. ## Parameters
  13. | Parameter | Description |
  14. |-----------|-------------|
  15. | `table_name` | The name of the table with the constraint you want to drop. |
  16. | `name` | The name of the constraint you want to drop. |
  17. ## Viewing Schema Changes <span class="version-tag">New in v1.1</span>
  18. {% include {{ page.version.version }}/misc/schema-change-view-job.md %}
  19. ## Example
  20. ~~~ sql
  21. > SHOW CONSTRAINTS FROM orders;
  22. ~~~
  23. ~~~
  24. +--------+---------------------------+-------------+-----------+----------------+
  25. | Table | Name | Type | Column(s) | Details |
  26. +--------+---------------------------+-------------+-----------+----------------+
  27. | orders | fk_customer_ref_customers | FOREIGN KEY | customer | customers.[id] |
  28. | orders | primary | PRIMARY KEY | id | NULL |
  29. +--------+---------------------------+-------------+-----------+----------------+
  30. ~~~
  31. ~~~ sql
  32. > ALTER TABLE orders DROP CONSTRAINT fk_customer_ref_customers;
  33. ~~~
  34. ~~~
  35. ALTER TABLE
  36. ~~~
  37. ~~~ sql
  38. > SHOW CONSTRAINTS FROM orders;
  39. ~~~
  40. ~~~
  41. +--------+---------+-------------+-----------+---------+
  42. | Table | Name | Type | Column(s) | Details |
  43. +--------+---------+-------------+-----------+---------+
  44. | orders | primary | PRIMARY KEY | id | NULL |
  45. +--------+---------+-------------+-----------+---------+
  46. ~~~
  47. {{site.data.alerts.callout_info}}You cannot drop the <code>primary</code> constraint, which indicates your table's <a href="primary-key.html">Primary Key</a>.{{site.data.alerts.end}}
  48. ## See Also
  49. - [`DROP COLUMN`](drop-column.html)
  50. - [`DROP INDEX`](drop-index.html)
  51. - [`ALTER TABLE`](alter-table.html)