4
votes

This is my query:

create table if not exists applications (
    idResearch int not null, 
    idProf char(10) not null,
    primary key (idResearch, idProf),
    foreign key (idResearch) references research(idResearch),
    foreign key (idProf) references professor(idProf)
);

and this is the table:

INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('10', '123456789');
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('10', '432156789');
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('10', '789654321');
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('11', '876098432');
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('11', '234567890');
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('12', '432156789');
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('12', '789654321');
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('13', '876098432');
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('14', '876098432');
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('14', '987654321');
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('15', '432156789');
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('16', '123456789');
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('16', '432156789');
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('16', '789654321');
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('17', '789654321');
INSERT INTO `university`.`applications` (`idResearch`, `idProf`) VALUES ('17', '432156789');

15:26:13 create table if not exists applications ( idResearch int not null, idProf char(10) not null, primary key (idResearch, idProf), foreign key (idResearch) references research(idResearch), foreign key (idProf) references professor(idProf) ) Error Code: 1215. Cannot add foreign key constraint 0.109 sec

2
And where are you getting the error? Note: I am a big fan of id columns being auto-incremented columns. - Gordon Linoff
0 118 15:01:50 INSERT INTO university.applications (idResearch, idProf) VALUES ('10', '123456789') Error Code: 1054. Unknown column 'idProf' in 'field list' 0.000 sec - Roy Azikri
I don't see anything wrong with your posted INSERT statement. are you sure there is no special character? - Rahul
what do you mean by special character? I don't think so - Roy Azikri
Your code (with minor modifications) works on SQL Fiddle (sqlfiddle.com/#!9/25331). I suspect you have a bad character in the create table statement. Or two databases with different table structures. - Gordon Linoff

2 Answers

0
votes

There is no problem with the syntax in your query, it could be data issue.

Before executing the insert the query, check the following

  1. Table 'research' & column 'idResearch' exist
  2. Table 'professor' & column 'idProf' exist
  3. The reference values (in the insert query) for the columns idResearch, idProf should exist in the research and professor tables resp.

Truncate\delete and drop the 'applications' table manually and then create it once again. Then execute your insert query.

0
votes

I would change the following in the foreign key statements:

constraint FKResearch foreign key (idResearch) references research(idResearch),
constraint FKProfessor foreign key (idProf) references professor(idProf)

This way, when it encounters an error, MySQL will tell you which of those foreign key constraints is being violated.

Once you know that, you have to check whether the column names are matching, the values you are trying to insert actually have a corresponding father-value in the referenced table, and if the definition of both columns is exactly the same (check the collation and the encoding, most of the times the problem lays there).