Home >Database >Mysql Tutorial >Why Am I Getting MySQL Foreign Key Error 1005 (errno 150)?

Why Am I Getting MySQL Foreign Key Error 1005 (errno 150)?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-07 10:01:20523browse

Why Am I Getting MySQL Foreign Key Error 1005 (errno 150)?

Understanding MySQL Foreign Key Error 1005

When creating a foreign key constraint, MySQL encounters an error 1005 accompanied by an "errno 150" message. This error typically arises when the referenced table lacks a usable index that aligns with the referenced columns.

Answering Your Questions:

  1. Can a Primary Key also serve as a Foreign Key?
    No, a primary key cannot serve as a foreign key for the same table, as it would result in circular referencing.
  2. Addressing the MySQL Error:
    The error message suggests that the "dbimmobiliCondoni" table is missing an index on the referenced columns. To resolve this, you should create the following index on the "dbimmobiliImmobile" table:
CREATE INDEX idx_immobile ON dbimmobili.Immobile (ComuneImmobile, ViaImmobile, CivicoImmobile, InternoImmobile);

The above is the detailed content of Why Am I Getting MySQL Foreign Key Error 1005 (errno 150)?. 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