Skip to content
Advertisement

#1005 – Can’t create table `musicplayer`.`Albums` (errno: 150 “Foreign key constraint is incorrectly formed”)

I’m trying to define some tables with SQL, but when I execute it I have an error. Below there is my code and the error which I receive.

Error in my SQL query:

MySQL said: Documentation

1005 – Can’t create table musicplayer.Albums (errno: 150 “Foreign key constraint is incorrectly formed”) (Details…)

Advertisement

Answer

Declare the tables in order, so the tables are defined before they are referenced:

Here is a db<>fiddle.

You can still have tables that reference each other. However, you will need to declare such foreign key constraints using ALTER TABLE rather than in the CREATE TABLE statement. However, you do not need to do this for these table definitions.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement