Home > Database > Mysql Tutorial > Why Am I Getting 'Can't create table ... (errno: 150)' When Adding a Foreign Key?

Why Am I Getting 'Can't create table ... (errno: 150)' When Adding a Foreign Key?

Mary-Kate Olsen
Release: 2024-12-04 19:27:12
Original
427 people have browsed it

Why Am I Getting

Error Adding Foreign Key: Understanding and Resolving

When attempting to add a foreign key to an existing table named "katalog," users may encounter an error similar to "Can't create table 'mytable.#sql-7fb1_7d3a' (errno: 150)." This error can be resolved by following a specific process.

The query to add the foreign key should be in the following format:

ALTER TABLE <table_name> ADD FOREIGN KEY (<column_name>) REFERENCES <referenced_table_name> (<referenced_column_name>) ON DELETE <delete_action> ON UPDATE <update_action>;
Copy after login

In this case, the correct query would be:

ALTER TABLE katalog ADD FOREIGN KEY (Sprache) REFERENCES Sprache (ID) ON DELETE SET NULL ON UPDATE SET NULL;
Copy after login

Ensure that the following conditions are met:

  • The table and columns being referenced exist.
  • The data types of the columns match.
  • The referenced table has a primary key or unique index.

If these conditions are fulfilled and the error persists, it may indicate other underlying issues that need to be addressed.

The above is the detailed content of Why Am I Getting 'Can't create table ... (errno: 150)' When Adding a 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