MySQL problem: Error 1215: Can not add foreign key constraint

0

I have two Foreigns Keys and I can not get them inserted, I get an error. Any ideas:

mysql> create table Inscritos(
-> id int(6) not null auto_increment,
-> estudianteId int(6) unsigned not null,
-> tutorialId int(6) unsigned not null,
-> primary key(id),
-> index (estudianteId),
-> index (tutorialId),
-> foreign key(estudianteId) references Student(id),
-> foreign key(tutorialId) references Tutorials(id)
-> );
  

ERROR 1215 (HY000): Can not add foreign key constraint

The creation of the Student table is the following:

mysql> create table Student(
-> id int(6) unsigned auto_increment primary key,
-> firstname varchar(30) not null,
-> lastname varchar(30) not null
-> );  

And the creation of the Tutorials table is:

mysql> create table Tutorials(
-> id int(6) auto_increment primary key,
-> title varchar(100) not null,
-> author varchar(40) not null,
-> date DATE 
-> );  
    
asked by Alexander Silva Guerrero 11.06.2018 в 20:20
source

2 answers

0

For the foreign keys you need the fields to be identical, the problem I see here is that in the table Tutorials the field id is int (6) but in Inscritos is defined as tutorialId int (6) unsigned not null.

Try creating Tutorials with id unsigned as in Inscritos

Greetings

    
answered by 11.06.2018 в 22:43
0

I found the problem, in:
    tutorialId int (6) unsigned not null,
The "unsigned" should not go.

Same thanks to everyone for their comments.

    
answered by 11.06.2018 в 22:51