// tables reference

ALTER TABLE DROP FOREIGN KEY

Removes the reference check and nothing else: the data stays, the columns stay. Every engine drops by name; MySQL has its own clause (8.0.19+ also takes DROP CONSTRAINT) and leaves a souvenir index.

类别
表
In this category
10 of 16

语法

ALTER TABLE t DROP CONSTRAINT fk_name  -- SQL Server, PostgreSQL, Oracle, MySQL 8.0.19+; SQL Server takes IF EXISTS from 2016
ALTER TABLE t DROP FOREIGN KEY fk_name  -- MySQL's own clause, the only form before 8.0.19; no IF EXISTS
-- SQLite: no DROP CONSTRAINT; rebuild the table without the reference

Each block is a separate form. The support table and the examples show which engine takes which.

引擎支持

引擎Support备注
SQL Server 是 DROP CONSTRAINT fk_name; find generated names in sys.foreign_keys
PostgreSQL 是 DROP CONSTRAINT fk_name; no dependants to worry about, a foreign key is a leaf
MySQL 是 DROP FOREIGN KEY fk_name; the index InnoDB built for it stays
SQLite No equivalent the reference is part of the table definition; rebuild to remove it
Oracle 是 DROP CONSTRAINT fk_name, by name

示例

SQL Server

-- find the name, then drop
SELECT fk.name
FROM sys.foreign_keys AS fk
WHERE fk.parent_object_id = OBJECT_ID(N'dbo.orders');
ALTER TABLE dbo.orders DROP CONSTRAINT fk_orders_customer;

PostgreSQL

ALTER TABLE orders DROP CONSTRAINT fk_orders_customer;

MySQL

-- drop the constraint, then the index if you do not want it
ALTER TABLE orders DROP FOREIGN KEY fk_orders_customer;
ALTER TABLE orders DROP INDEX fk_orders_customer;

Oracle

ALTER TABLE orders DROP CONSTRAINT fk_orders_customer;

SQLite

-- rebuild without the FOREIGN KEY clause; run PRAGMA foreign_keys = OFF
-- before BEGIN (it is a no-op inside a transaction), then wrap the copy in one

值得了解

  • MySQL's DROP FOREIGN KEY leaves the index InnoDB created for the constraint in place; it is retired only if you later build another index that can serve a foreign key on those columns. The reverse fails while the constraint lives: MySQL refuses to drop an index the constraint still needs.
  • The name is the whole game: four of five engines generate names for unnamed constraints (MySQL's look like orders_ibfk_1, SQL Server's like FK__orders__customer__7). Find them before scripting the drop; sys.foreign_keys, information_schema and user_constraints all list them.
  • Dropping the constraint changes no data: rows that already point at missing parents keep pointing. If the goal was to delete parents the constraint was blocking, the orphan cleanup is on you afterwards.
  • The drop itself is catalog-only, but re-adding the constraint later validates every existing row (PostgreSQL reads the referencing and the referenced table in full to do it), so a drop-then-re-add cycle on a large table is not free. On PostgreSQL, re-add with NOT VALID and run VALIDATE CONSTRAINT later: the scan is the same size, but it holds only a SHARE UPDATE EXCLUSIVE lock, so writes keep running during the check.

Questions about DROP FOREIGN KEY

How do I find a foreign key's name?

SQL Server: sys.foreign_keys filtered by parent_object_id. MySQL: SHOW CREATE TABLE orders, or information_schema.referential_constraints. PostgreSQL: information_schema or pg_constraint WHERE contype = 'f'. Oracle: user_constraints WHERE constraint_type = 'R'.

Why can I not drop the index on my MySQL foreign key column?

InnoDB needs an index on the referencing columns to enforce the constraint, so it blocks the DROP INDEX while the constraint exists. Drop the foreign key first, then the index, or keep the index: it still speeds joins and lookups on that column.

Will dropping a foreign key delete or fix orphaned rows?

No. The statement is catalog-only: no rows are checked, changed or deleted. Rows referencing missing parents stay as they are, so run your own anti-join cleanup if that is why the constraint is going.

Browse every statement in the SQL DDL reference, or see what the SQLly desktop app does for your database.

// dialect-aware editing

DDL that matches the engine you're connected to

SQLly checks your statements against the dialect of the open connection, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle, so an ALTER TABLE that works on one engine gets flagged before it runs on another.