Home  >  Article  >  Database  >  Why Does MySQL Throw Error 150: Foreign Key Reference Type Mismatch?

Why Does MySQL Throw Error 150: Foreign Key Reference Type Mismatch?

DDD
DDDOriginal
2024-11-19 18:06:03403browse

Why Does MySQL Throw Error 150: Foreign Key Reference Type Mismatch?

MySQL Error 150: Foreign Key Reference Type Mismatch

While attempting to create a database with several tables, a user encountered an error: "Error Code: 1005. Can't create table '336_project.sections' (errno: 150)." This error is related to the use of foreign keys.

Upon closer examination, it was discovered that the data types of the related columns did not match. In this case, the "Course_Code" column in the "Sections" table was defined as a VARCHAR, while the corresponding column in the "Courses" table was defined as an INT.

MySQL requires that foreign keys and primary keys in related tables have matching data types. This error occurs when the data types do not match, even if the names and syntax of the foreign key constraints appear correct.

Solution:

To resolve this error, ensure that the data types of the related columns in the parent and child tables are identical. In this example, the data type of the "Course_Code" column in both the "Sections" and "Courses" tables should be changed to VARCHAR.

Once the data types are properly matched, the foreign key constraint will be enforced, and the table creation will succeed without error.

The above is the detailed content of Why Does MySQL Throw Error 150: Foreign Key Reference Type Mismatch?. 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