1
When creating this code always comes the error:
1215 "Cannot add Foreign key Constraint"
I’ve tried everything, checked the foreign key restrictions and nothing. Someone can help me?
create table alunodisc (
codigodisc int,
mataluno int ,
constraint PK primary key(codigodisc,mataluno),
constraint codalundisc foreign key (codigodisc) references disciplinas(codisc),
constraint matalundisc foreign key (mataluno) references alunos(mataluno)
);
Which database engine? SQL Server? Mysql? Sqlite?
– Jefferson Quesado
I use the Mysql.
– Bruno Bugre
https://www.scriptbrasil.com.br/forum/topic/176772-resolvido-error-1215-cannot-add-foreign-key-constraint/
– Motta
Place the table structure
alunos
anddisciplinas
.– Roberto de Campos
The error is stating that you cannot add a Foreign Key Constraint, I believe you are referencing a wrong column name. 1 - Note how the column name of each table is written. 2 - Make sure you put primary keys in the columns you are referencing. 3 - Check that the column names are equal in the reference. Anyway, everything indicates that it is typing error, the bad of mysql and other banks that it can’t show exactly where the error is, you have to check line by line and sometimes the person passes by.
– Danillo Victtor