Home >Database >Mysql Tutorial >Why Am I Getting MySQL Error 1215: Cannot Add Foreign Key Constraint?

Why Am I Getting MySQL Error 1215: Cannot Add Foreign Key Constraint?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-28 04:37:10136browse

Why Am I Getting MySQL Error 1215: Cannot Add Foreign Key Constraint?

Unable to Add Foreign Key: MySQL Error 1215

In an attempt to engineer a new database schema, a user encountered the MySQL error "Error 1215: Cannot add foreign key constraint." They have already set the database engine to InnoDB and verified that the keys designated as foreign keys are primary keys in their respective tables.

The issue may stem from a difference in data types between the foreign key columns in Clients_has_Staff and the corresponding primary key columns in the parent tables (Clients and Staff).

Possible Solution:

The user should check if the following columns have the same data type in all three tables:

  • Clients_has_Staff.Clients_Case_Number
  • Clients.Case_Number
  • Clients_has_Staff.Staff_Emp_ID
  • Staff.Emp_ID

For example, if the parent table columns are INT UNSIGNED, the foreign key columns in Clients_has_Staff must also be INT UNSIGNED. Failure to match data types will prevent the foreign key constraint from being added successfully.

The above is the detailed content of Why Am I Getting MySQL Error 1215: Cannot Add Foreign Key Constraint?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn