MySQL Errno 150: Unveiling the Foreign Key Discrepancy
In an attempt to establish relationships between tables, you encountered an elusive foreign key error with code 150. Let's delve into the intricacies of your SQL script to pinpoint the source of this issue.
Upon closer examination, the error can be attributed to a fundamental difference in data types between the foreign key and its referenced column. Within the "Sections" table, you have defined the "Instructor_ID" column as "varchar(10)" while the primary key of the referenced "Instructors" table is "ID" with a data type of "varchar(10)." Although the lengths match, the data types are distinct.
This discrepancy creates a mismatch between the columns that should be linked. MySQL requires that the foreign key column matches the data type of the referenced primary key column. In this case, you should modify the "Instructor_ID" column in the "Sections" table to match the data type of the "ID" column in the "Instructors" table, which is "varchar(10)."
Once you ensure that the foreign key column type aligns with the referenced primary key column type, MySQL should be able to establish the foreign key relationship without encountering the Errno 150 error.
The above is the detailed content of MySQL Error 150: Why is my foreign key failing even with matching column lengths?. For more information, please follow other related articles on the PHP Chinese website!