SQL Keywords

SQL DROP CONSTRAINT

What is SQL DROP CONSTRAINT?

Removes an existing constraint from a table or domain, altering the schema without touching the data.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL DROP CONSTRAINT: PostgreSQL, SQL Server, Oracle, IBM Db2, Snowflake, Redshift support full syntax. MySQL and MariaDB support it partially through DROP PRIMARY KEY / DROP FOREIGN KEY / DROP CHECK. SQLite uses ALTER TABLE ... DROP CONSTRAINT only from version 3.35+ (check support).

SQL DROP CONSTRAINT Full Explanation

SQL DROP CONSTRAINT is used within an ALTER TABLE or ALTER DOMAIN statement to delete a previously defined rule such as PRIMARY KEY, UNIQUE, CHECK, FOREIGN KEY, or EXCLUDE. Removing the constraint changes how the database enforces data integrity going forward but does not modify existing rows unless CASCADE is specified and the database performs additional actions (for example, dropping dependent indexes).Standard SQL defines the syntax ALTER TABLE table_name DROP CONSTRAINT constraint_name, with optional behavioral clauses like RESTRICT (default) or CASCADE. Some dialects extend this with IF EXISTS, ALTER DOMAIN DROP CONSTRAINT, or allow constraint type–specific shortcuts (e.g., MySQL’s DROP PRIMARY KEY, DROP FOREIGN KEY). Key points:- The statement requires ownership or ALTER privileges on the table.- Dropping a constraint referenced by other objects (indexes, foreign-key dependencies, views) may be blocked unless CASCADE is supplied.- Constraints created implicitly by the system (for example, indexes backing PRIMARY KEY or UNIQUE) are typically removed automatically.- NOT NULL constraints are dropped with ALTER TABLE ALTER COLUMN column_name DROP NOT NULL rather than DROP CONSTRAINT.- Always verify that application logic or other tables do not rely on the constraint before removal.

SQL DROP CONSTRAINT Syntax

ALTER TABLE table_name
    DROP CONSTRAINT constraint_name [CASCADE | RESTRICT];

-- Dialect extensions
ALTER TABLE table_name DROP CONSTRAINT IF EXISTS constraint_name;
ALTER DOMAIN domain_name DROP CONSTRAINT constraint_name;

SQL DROP CONSTRAINT Parameters

  • table_name (identifier) - The target table that owns the constraint.
  • constraint_name (identifier) - The name of the constraint to remove.
  • CASCADE (keyword) - Automatically drop objects that depend on the constraint.
  • RESTRICT (keyword) - Refuse to drop if any dependent objects exist (default).
  • IF EXISTS – keyword (dialect specific) - Do nothing if the constraint is absent.

Example Queries Using SQL DROP CONSTRAINT

-- 1. Remove an obsolete foreign key
ALTER TABLE orders
    DROP CONSTRAINT fk_orders_customer_id;

-- 2. Drop a UNIQUE constraint that prevents duplicate emails
ALTER TABLE users
    DROP CONSTRAINT uq_users_email;

-- 3. PostgreSQL: force removal along with dependent objects
ALTER TABLE inventory
    DROP CONSTRAINT pk_inventory_id CASCADE;

-- 4. MySQL equivalent for foreign key
ALTER TABLE orders
    DROP FOREIGN KEY fk_orders_customer_id;

Expected Output Using SQL DROP CONSTRAINT

  • The specified constraint is removed
  • The database updates system catalogs, deletes any supporting index if applicable, and no longer enforces that rule on future data modifications
  • If CASCADE is used, dependent objects are also dropped

Use Cases with SQL DROP CONSTRAINT

  • Retiring legacy rules after a data-model redesign.
  • Allowing duplicate values by dropping a UNIQUE or PRIMARY KEY before bulk load, then recreating it.
  • Removing a FOREIGN KEY to break a circular dependency during migrations.
  • Deleting an overly strict CHECK constraint that causes valid data to be rejected.

Common Mistakes with SQL DROP CONSTRAINT

  • Omitting ALTER TABLE and writing DROP CONSTRAINT directly (not valid SQL).
  • Misspelling the constraint name; use catalog queries or \d in psql to confirm.
  • Forgetting dependent objects exist and receiving an error: cannot drop because other objects depend on it.
  • Trying to drop NOT NULL with DROP CONSTRAINT instead of ALTER COLUMN ... DROP NOT NULL.
  • Expecting data cleanup: DROP CONSTRAINT does not fix existing invalid rows.

Related Topics

ALTER TABLE, ADD CONSTRAINT, CREATE TABLE, DROP INDEX, ALTER COLUMN, CASCADE

First Introduced In

SQL-92

Frequently Asked Questions

How do I safely drop a constraint referenced by other objects?

Use CASCADE to let the database remove dependent objects automatically, or remove the dependencies manually first and rerun DROP CONSTRAINT.

Can I drop a NOT NULL constraint with DROP CONSTRAINT?

No. NOT NULL is modified with ALTER TABLE ALTER COLUMN column_name DROP NOT NULL.

Will dropping a PRIMARY KEY remove its index?

Yes. The supporting unique index is deleted automatically in most databases.

Does DROP CONSTRAINT affect historical data?

No. It only stops future enforcement. Existing rows stay as they are, even if they break the former rule.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!