// SQL Server error reference

SQL Server error 3726: Can't drop: referenced by a FOREIGN KEY

DROP TABLE failed because another table's foreign key references it. Drop the referencing constraint (or that table) first.

Error number
3726
Severity
16
Category
Constraints and keys
Error message
Could not drop object '%.*ls' because it is referenced by a FOREIGN KEY constraint.

What it means

SQL Server won't drop a table that is the parent of a foreign key, because the child table's constraint would point to nothing. The message names the table; the foreign keys that reference it are listed in sys.foreign_keys. TRUNCATE TABLE on such a table fails too, with error 4712.

Common causes

  • Dropping tables in the wrong order in a teardown script.
  • Recreating a table during a migration while other tables still reference it.

How to fix it

  1. Find the foreign keys that reference the table.

    SELECT fk.name, OBJECT_SCHEMA_NAME(fk.parent_object_id) AS child_schema,
           OBJECT_NAME(fk.parent_object_id) AS child_table
    FROM sys.foreign_keys AS fk
    WHERE fk.referenced_object_id = OBJECT_ID(N'dbo.Customers');
  2. Drop those constraints (or the child tables) first, then the table.

    ALTER TABLE dbo.Orders DROP CONSTRAINT FK_Orders_Customers;
    DROP TABLE dbo.Customers;
  3. In teardown scripts, drop child tables before parents.

Related errors

More constraints and keys errors

The code and message text are from the Microsoft Database Engine events and errors reference. Browse the full SQL Server error list or search every database error.

// fewer errors, faster fixes

Work on SQL Server in SQLly

A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for SQL Server and the other databases you use.