Error: 1215 cannot add Foreign key Constraint -Mysql 5.7

Asked

Viewed 310 times

2

I am trying to create a table but I have a problem in a foreign key, at least it seems and is what I could understand in my research.

Look at the code please, the error occurs in the last table.
(I found several similar posts but tried the suggestions and nothing helped)

CREATE TABLE Professor(
    ra INT  NOT NULL 
    ,apelido VARCHAR(30) NOT NULL
    ,nome VARCHAR(120) NOT NULL
    ,email VARCHAR(80) NOT NULL
    ,celular CHAR(11) NOT NULL
    ,CONSTRAINT pkra PRIMARY KEY (ra)
    ,CONSTRAINT uqapelido UNIQUE (apelido) 
    );

CREATE TABLE Curso(
    sigla VARCHAR(5) NOT NULL 
    ,nome VARCHAR(50) NOT NULL
    ,CONSTRAINT pksigla PRIMARY KEY (sigla)
    ,CONSTRAINT uqnome UNIQUE (nome)
    );

CREATE TABLE GradeCurricular(
    sigla_curso VARCHAR(5) NOT NULL
    ,ano SMALLINT NOT NULL
    ,semestre CHAR(1) NOT NULL
    ,CONSTRAINT fksigla_curso FOREIGN KEY (sigla_curso) REFERENCES Curso (sigla) 
    ,PRIMARY KEY (sigla_curso,ano,semestre)
);

CREATE TABLE Periodo(
    sigla_curso VARCHAR(5)
    ,ano_grade SMALLINT
    ,semestre_grade CHAR (1) 
    ,numero TINYINT NOT NULL
    ,CONSTRAINT fk_sigla_curso FOREIGN KEY (sigla_curso) REFERENCES GradeCurricular (sigla_curso)
    ,CONSTRAINT fk_ano_grade FOREIGN KEY (ano_grade) REFERENCES GradeCurricular (ano)
    ,CONSTRAINT fk_semestre_grade FOREIGN KEY (semestre_grade) REFERENCES GradeCurricular (semestre)
    ,PRIMARY KEY (sigla_curso,ano_grade,semestre_grade,numero)
);    

1 answer

0


Use this statement for the Period table:

CREATE TABLE Periodo(
    sigla_curso VARCHAR(5)
    ,ano_grade SMALLINT
    ,semestre_grade CHAR (1) 
    ,numero TINYINT NOT NULL
    ,CONSTRAINT fk_sigla_curso FOREIGN KEY (sigla_curso, ano_grade, semestre_grade) REFERENCES GradeCurricular (sigla_curso, ano, semestre)
    ,PRIMARY KEY (sigla_curso,ano_grade,semestre_grade,numero)
);
  • Opaa Man, It worked!!! Thanks, I spent all day trying various things here and unsuccessfully rs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.