// MySQL error reference

MySQL error 1826: Duplicate foreign key constraint name

A foreign key's name is already used. In MySQL foreign key names must be unique across the whole database, not just the table.

Error number
1826
Name
ER_FK_DUP_NAME
SQLSTATE
HY000
Category
Keys and constraints
Error message
Duplicate foreign key constraint name '%s'

What it means

InnoDB stores foreign key constraint names per database, so two tables can't each have a constraint called fk_customer. Creating the second one fails with 1826.

Common causes

  • Copying a table definition, including its constraint names, to create a similar table.
  • Naming conventions that don't include the table name.

How to fix it

  1. Find where the name is used.

    SELECT table_name, constraint_name FROM information_schema.referential_constraints
    WHERE constraint_schema = DATABASE() AND constraint_name = 'fk_customer';
  2. Use names that include the table, such as fk_orders_customer and fk_invoices_customer, or leave the name out and let MySQL generate one.

Related errors

More keys and constraints errors

The code and message text are from the MySQL 8.4 Error Message Reference. Browse the full MySQL error list or search every database error.

// fewer errors, faster fixes

Work on MySQL in SQLly

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