As an addition to a 2 foreign key that are both primary?

2

My problem is as follows, I have 2 COORDINATION and NACCLE_ACADEMIC tables, the relationship between them is N: M (many to many), the structure of the table COORDINATION and NACCLEO_ACADÉMICO the table is as follows:

CREATE TABLE coordinacion (
idcoord int(11) NOT NULL,
nomcoord varchar(50) NOT NULL,
ubicacion varchar(20) NOT NULL,
CONSTRAINT pk_coordinacion primary key(idcoord)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE nucleo_academico (
idnucleo int(11) NOT NULL,
nomnucleo varchar(150) NOT NULL,
CONSTRAINT pk_nucleo_academico primary key(idnucleo)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

So far so good, the tables are created normally, Now when I proceed to create the table of the relationship OCUPA to store the respective fields, I throw this error "# 1215 - No can add foreign key constraint ", Here is the structure of the table OCUPA:

CREATE TABLE ocupa(
idcoord int(11) NOT NULL,
idnucleo int(11) NOT NULL,
foreign key(idcoord) REFERENCES coordinacion(idcoord),
foreign key(idnucleo) REFERENCES coordinacion(idnucleo),
primary key(idcoord,idnucleo)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

I would be very grateful if you would give me a hand with this problem, I have already changed several things and corrected several errors, I just need to correct this to continue advancing with my project ...

    
asked by Frankis Anthony 19.09.2018 в 04:09
source

1 answer

2

The error is quite simple, the column idnucleo does not exist in the table coordinacion , referenced when assigning its foreign key , you should refer to the table nucleo_academico in which if that column exists.

CREATE TABLE ocupa(
idcoord int(11) NOT NULL,
idnucleo int(11) NOT NULL,
foreign key(idcoord) REFERENCES coordinacion(idcoord),
foreign key(idnucleo) REFERENCES nucleo_academico(idnucleo),
primary key(idcoord,idnucleo)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    
answered by 19.09.2018 / 04:25
source