Error
Error Code:
1821
MySQL Error 1821: Missing Foreign Key Index
Description
This error occurs when you attempt to create a foreign key constraint, but the referenced column(s) in the parent table (referred to as the 'foreign table' in the message) do not have an index. MySQL requires an index on these columns to efficiently enforce referential integrity and perform necessary lookups.
Error Message
Failed to add the foreign key constraint. Missing index for constraint '%s' in the foreign table '%s'
Known Causes
3 known causesMissing Index on Referenced Columns
The columns in the parent table that the foreign key constraint is intended to reference do not have any index defined on them.
Incompatible Composite Index
If the foreign key involves multiple columns, an existing composite index on the parent table's referenced columns might not cover all required columns or be in the correct order.
Temporary Table Restrictions
Foreign key constraints cannot be created on temporary tables, and attempting to do so may implicitly lead to this error if the referenced table is temporary.
Solutions
3 solutions available1. Add Index to Foreign Table Column easy
Manually create an index on the column in the foreign table that is referenced by the foreign key.
1
Identify the foreign table and the column that the foreign key constraint is referencing. This information is usually available in the error message itself (e.g., '%s' in the foreign table '%s').
2
Connect to your MySQL server and select the database containing the foreign table.
USE your_database_name;
3
Create an index on the referenced column in the foreign table. The column name should match the column in the parent table that the foreign key points to.
CREATE INDEX index_name ON foreign_table_name (referenced_column_name);
4
Attempt to add the foreign key constraint again.
ALTER TABLE child_table_name ADD CONSTRAINT constraint_name FOREIGN KEY (child_column_name) REFERENCES foreign_table_name (referenced_column_name);
2. Create Index During Foreign Key Creation easy
Specify index creation directly when adding the foreign key constraint.
1
Connect to your MySQL server and select the database.
USE your_database_name;
2
When defining the foreign key constraint, MySQL will often automatically create the necessary index on the referencing column in the child table. However, this error indicates a missing index on the *referenced* column in the *parent* table. To ensure the index is created on the foreign table's referenced column, you can explicitly add it before creating the foreign key.
CREATE INDEX index_name ON foreign_table_name (referenced_column_name);
3
Then, add the foreign key constraint.
ALTER TABLE child_table_name ADD CONSTRAINT constraint_name FOREIGN KEY (child_column_name) REFERENCES foreign_table_name (referenced_column_name);
3. Recreate Table with Indexes medium
A more comprehensive solution involving recreating the foreign table with the necessary index.
1
Back up the data from the foreign table.
SELECT * INTO OUTFILE '/path/to/backup/foreign_table_backup.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM foreign_table_name;
2
Drop the foreign table.
DROP TABLE foreign_table_name;
3
Recreate the foreign table, ensuring to define an index on the column that will be referenced by the foreign key. This column is the primary key or a unique key in the `foreign_table_name`.
CREATE TABLE foreign_table_name (
referenced_column_name INT PRIMARY KEY AUTO_INCREMENT,
-- other columns...
INDEX idx_referenced_column (referenced_column_name)
);
4
Restore the data into the recreated foreign table.
LOAD DATA INFILE '/path/to/backup/foreign_table_backup.csv' INTO TABLE foreign_table_name FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n';
-- Adjust column mapping if necessary.
5
Now, attempt to add the foreign key constraint to the child table.
ALTER TABLE child_table_name ADD CONSTRAINT constraint_name FOREIGN KEY (child_column_name) REFERENCES foreign_table_name (referenced_column_name);