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>;
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;
Ensure that the following conditions are met:
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!