Home > Database > Mysql Tutorial > MySQL Error 150: Why Can\'t I Create This Foreign Key?

MySQL Error 150: Why Can\'t I Create This Foreign Key?

Barbara Streisand
Release: 2024-11-27 05:19:14
Original
832 people have browsed it

MySQL Error 150: Why Can't I Create This Foreign Key?

MySql Encountering Error 150: Deciphering Foreign Key Woes

When attempting to create a table with a foreign key referencing another table, developers may encounter error 150, baffling them with its cryptic message. This article delves into the underlying cause of this error and provides a solution.

As detailed in the MySQL documentation, error 150 occurs when recreating a table that was previously dropped does not adhere to the foreign key constraints referencing it. Specifically, the table must maintain the same column names, types, and indexes on the referenced keys as when it was originally created.

To resolve this issue, ensure that the table containing the foreign key (in this case, "foo") is also created as an InnoDB table. As per the MySQL documentation, both tables involved in a foreign key relationship must be InnoDB tables and not temporary tables.

Therefore, to correct the error, try recreating the "foo" table as an InnoDB table using the following query:

CREATE TABLE foo (
  id INT PRIMARY KEY
) ENGINE = InnoDB;
Copy after login

Once the "foo" table is created as an InnoDB table, you should be able to successfully create the "bar" table with the foreign key reference without encountering error 150.

The above is the detailed content of MySQL Error 150: Why Can\'t I Create This Foreign Key?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template