错误代码MySQL Workbench:1215无法添加外键约束 [英] Error Code MySQL Workbench: 1215 Cannot add foreign key constraint

查看:124
本文介绍了错误代码MySQL Workbench:1215无法添加外键约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CREATE TABLE Estudante (
Matrícula int,
RG int,
Nome varchar(20),
Email varchar(20),
Endereço_atual  varchar(20),
Telefone_atual varchar(20),
Endereço_permanente varchar(20),
Telefone_permanente varchar(20),
Data_de_nascimento date,
Sexo varchar(1),
Série varchar(3),
Grau varchar(10),
Curso varchar(20),
PRIMARY KEY(Matrícula,RG)
);

CREATE TABLE Professor (
Matrícula int,
RG int,
CPF int,
Nome varchar(10),
Classificação varchar(3),
Endereço varchar(20),
Email varchar(10),
PRIMARY KEY(Matrícula,RG,CPF)
);

CREATE TABLE Departamento (
Nome varchar(20),
Código_departamento int,
Matrícula_prof int,
Ramal varchar(10),
Campus varchar(10),
PRIMARY KEY(Nome,Código_departamento),
CONSTRAINT FK_MatriculaProf FOREIGN KEY(Matrícula_prof) REFERENCES Professor (Matrícula)
);

CREATE TABLE Disciplina (
Código int,
Departamento_resp int,
Nome varchar(20),
Descrição varchar(20),
Carga_horária varchar(10),
Duração varchar(10),
Nível varchar(10),
PRIMARY KEY(Código),
CONSTRAINT FK_DepartamentoResp FOREIGN KEY(Departamento_resp) REFERENCES Departamento (Código_departamento)
);

CREATE TABLE Turma (
ID_Turma int,
Disciplina int,
Matricula_Instrutor int,
Período_aplicação varchar(10),
Ano int,
PRIMARY KEY(ID_Turma),
CONSTRAINT FK_Disciplina FOREIGN KEY(Disciplina) REFERENCES Disciplina (Código),
CONSTRAINT FK_MatriculaInstrutor FOREIGN KEY(Matricula_Instrutor) REFERENCES Professor (Matrícula)
);

CREATE TABLE Relatório_Notas (
Matricula_Estudante int,
Turma int,
Disciplina int,
Notas int,
PRIMARY KEY(Matricula_Estudante),
CONSTRAINT FK_MatriculaEstudante FOREIGN KEY(Matricula_Estudante) REFERENCES Estudante (Matrícula,RG),
CONSTRAINT FK_Turma FOREIGN KEY(Turma) REFERENCES Turma (ID_Turma),
CONSTRAINT FK_Disciplina FOREIGN KEY(Disciplina) REFERENCES Disciplina (Código)
);

前3个表都可以,但是在"Disciplina"表上却出现此错误:错误代码MySQL Workbench:1215无法添加外键约束

The 3 first tables are ok, but on the "Disciplina" table I got this error: Error Code MySQL Workbench: 1215 Cannot add foreign key constraint

推荐答案

您将外键定义为

CONSTRAINT FK_DepartamentoResp FOREIGN KEY(Departamento_resp) REFERENCES Departamento (Código_departamento)

但是在父表上没有以Código_departamento列开头的索引.

but there is no index starting with column Código_departamento on the parent table.

如果将 Departamento 上的主键从(Nome,Código_departamento)更改为(Código_departamento,Nome)(反向列顺序)或在(Código_departamento)上添加一个额外的索引,一切正常.

If you change the primary key on Departamento from (Nome,Código_departamento) to (Código_departamento, Nome) (reverse column order) or add an extra index on (Código_departamento) things should work.

通常,在收到无法添加外键约束错误后,会出现 SHOW WARNINGS 显示有关该问题的更多详细信息.

In general a SHOW WARNINGS after receiving an Cannot add foreign key constraint error reveals more detailed information on the problem.

这篇关于错误代码MySQL Workbench:1215无法添加外键约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆