How do i solve 'Error Cannot add foreign key constraint'?










0















So I'm using sqlfiddle but I always get the error 'cannot add foreign key constraint'. Can somebody help me find the error? I'm sure the problem is on the first table (Professor), as if you delete both of the FK constraints, the code works perfectly.



CREATE TABLE Professor (
Inicials char(2) not null,
nom varchar(30),
DNI varchar(10),
Email varchar(30),
CONSTRAINT inicials_pk PRIMARY KEY (Inicials),
CONSTRAINT dni_professor UNIQUE (DNI),
CONSTRAINT Email_professor UNIQUE (Email)
);

INSERT INTO Professor (Inicials, nom, DNI, Email)
VALUES ('JF','Joel Ferragut','56783698V','jferragut@iesmontisa.org');
INSERT INTO Professor (Inicials, nom, DNI, Email)
VALUES ('AE','Aleix Esteve','56983698V','aesteve@iesmontisa.org');
INSERT INTO Professor (Inicials, nom, DNI, Email)
VALUES ('KC','Kevin Costes','56883698V','kevincostes@iesmontisa.org');

CREATE TABLE Grup (
Nivell varchar(10),
Curs varchar(10),
Lletra varchar(5),
Aula varchar(5),
CONSTRAINT GrupNivell_pk PRIMARY KEY (Nivell, Curs, Lletra)
);

INSERT INTO Grup (Nivell, Curs, Lletra, Aula)
VALUES ('1','2018-2019','A','24');

CREATE TABLE Docencia (
hores_totals int,
Inicials char(2) not null,
Nivell varchar(10),
Curs varchar(10),
Lletra varchar(5),
CONSTRAINT profe FOREIGN KEY (inicials)
REFERENCES Professors (inicials),
CONSTRAINT GrupNivell_pk FOREIGN KEY (Nivell, Curs, Lletra)
REFERENCES Grup (Nivell, Curs, Lletra)
);









share|improve this question






















  • Does creating the Professor table by itself cause the error as well?

    – Jim Jimson
    Nov 15 '18 at 11:26











  • @JimJimson Nope, the problem is the CONSTRAINTS on the table Professors. I'm quite sure about that.

    – Kevin Costes
    Nov 15 '18 at 11:28















0















So I'm using sqlfiddle but I always get the error 'cannot add foreign key constraint'. Can somebody help me find the error? I'm sure the problem is on the first table (Professor), as if you delete both of the FK constraints, the code works perfectly.



CREATE TABLE Professor (
Inicials char(2) not null,
nom varchar(30),
DNI varchar(10),
Email varchar(30),
CONSTRAINT inicials_pk PRIMARY KEY (Inicials),
CONSTRAINT dni_professor UNIQUE (DNI),
CONSTRAINT Email_professor UNIQUE (Email)
);

INSERT INTO Professor (Inicials, nom, DNI, Email)
VALUES ('JF','Joel Ferragut','56783698V','jferragut@iesmontisa.org');
INSERT INTO Professor (Inicials, nom, DNI, Email)
VALUES ('AE','Aleix Esteve','56983698V','aesteve@iesmontisa.org');
INSERT INTO Professor (Inicials, nom, DNI, Email)
VALUES ('KC','Kevin Costes','56883698V','kevincostes@iesmontisa.org');

CREATE TABLE Grup (
Nivell varchar(10),
Curs varchar(10),
Lletra varchar(5),
Aula varchar(5),
CONSTRAINT GrupNivell_pk PRIMARY KEY (Nivell, Curs, Lletra)
);

INSERT INTO Grup (Nivell, Curs, Lletra, Aula)
VALUES ('1','2018-2019','A','24');

CREATE TABLE Docencia (
hores_totals int,
Inicials char(2) not null,
Nivell varchar(10),
Curs varchar(10),
Lletra varchar(5),
CONSTRAINT profe FOREIGN KEY (inicials)
REFERENCES Professors (inicials),
CONSTRAINT GrupNivell_pk FOREIGN KEY (Nivell, Curs, Lletra)
REFERENCES Grup (Nivell, Curs, Lletra)
);









share|improve this question






















  • Does creating the Professor table by itself cause the error as well?

    – Jim Jimson
    Nov 15 '18 at 11:26











  • @JimJimson Nope, the problem is the CONSTRAINTS on the table Professors. I'm quite sure about that.

    – Kevin Costes
    Nov 15 '18 at 11:28













0












0








0








So I'm using sqlfiddle but I always get the error 'cannot add foreign key constraint'. Can somebody help me find the error? I'm sure the problem is on the first table (Professor), as if you delete both of the FK constraints, the code works perfectly.



CREATE TABLE Professor (
Inicials char(2) not null,
nom varchar(30),
DNI varchar(10),
Email varchar(30),
CONSTRAINT inicials_pk PRIMARY KEY (Inicials),
CONSTRAINT dni_professor UNIQUE (DNI),
CONSTRAINT Email_professor UNIQUE (Email)
);

INSERT INTO Professor (Inicials, nom, DNI, Email)
VALUES ('JF','Joel Ferragut','56783698V','jferragut@iesmontisa.org');
INSERT INTO Professor (Inicials, nom, DNI, Email)
VALUES ('AE','Aleix Esteve','56983698V','aesteve@iesmontisa.org');
INSERT INTO Professor (Inicials, nom, DNI, Email)
VALUES ('KC','Kevin Costes','56883698V','kevincostes@iesmontisa.org');

CREATE TABLE Grup (
Nivell varchar(10),
Curs varchar(10),
Lletra varchar(5),
Aula varchar(5),
CONSTRAINT GrupNivell_pk PRIMARY KEY (Nivell, Curs, Lletra)
);

INSERT INTO Grup (Nivell, Curs, Lletra, Aula)
VALUES ('1','2018-2019','A','24');

CREATE TABLE Docencia (
hores_totals int,
Inicials char(2) not null,
Nivell varchar(10),
Curs varchar(10),
Lletra varchar(5),
CONSTRAINT profe FOREIGN KEY (inicials)
REFERENCES Professors (inicials),
CONSTRAINT GrupNivell_pk FOREIGN KEY (Nivell, Curs, Lletra)
REFERENCES Grup (Nivell, Curs, Lletra)
);









share|improve this question














So I'm using sqlfiddle but I always get the error 'cannot add foreign key constraint'. Can somebody help me find the error? I'm sure the problem is on the first table (Professor), as if you delete both of the FK constraints, the code works perfectly.



CREATE TABLE Professor (
Inicials char(2) not null,
nom varchar(30),
DNI varchar(10),
Email varchar(30),
CONSTRAINT inicials_pk PRIMARY KEY (Inicials),
CONSTRAINT dni_professor UNIQUE (DNI),
CONSTRAINT Email_professor UNIQUE (Email)
);

INSERT INTO Professor (Inicials, nom, DNI, Email)
VALUES ('JF','Joel Ferragut','56783698V','jferragut@iesmontisa.org');
INSERT INTO Professor (Inicials, nom, DNI, Email)
VALUES ('AE','Aleix Esteve','56983698V','aesteve@iesmontisa.org');
INSERT INTO Professor (Inicials, nom, DNI, Email)
VALUES ('KC','Kevin Costes','56883698V','kevincostes@iesmontisa.org');

CREATE TABLE Grup (
Nivell varchar(10),
Curs varchar(10),
Lletra varchar(5),
Aula varchar(5),
CONSTRAINT GrupNivell_pk PRIMARY KEY (Nivell, Curs, Lletra)
);

INSERT INTO Grup (Nivell, Curs, Lletra, Aula)
VALUES ('1','2018-2019','A','24');

CREATE TABLE Docencia (
hores_totals int,
Inicials char(2) not null,
Nivell varchar(10),
Curs varchar(10),
Lletra varchar(5),
CONSTRAINT profe FOREIGN KEY (inicials)
REFERENCES Professors (inicials),
CONSTRAINT GrupNivell_pk FOREIGN KEY (Nivell, Curs, Lletra)
REFERENCES Grup (Nivell, Curs, Lletra)
);






mysql sql






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 11:25









Kevin CostesKevin Costes

51




51












  • Does creating the Professor table by itself cause the error as well?

    – Jim Jimson
    Nov 15 '18 at 11:26











  • @JimJimson Nope, the problem is the CONSTRAINTS on the table Professors. I'm quite sure about that.

    – Kevin Costes
    Nov 15 '18 at 11:28

















  • Does creating the Professor table by itself cause the error as well?

    – Jim Jimson
    Nov 15 '18 at 11:26











  • @JimJimson Nope, the problem is the CONSTRAINTS on the table Professors. I'm quite sure about that.

    – Kevin Costes
    Nov 15 '18 at 11:28
















Does creating the Professor table by itself cause the error as well?

– Jim Jimson
Nov 15 '18 at 11:26





Does creating the Professor table by itself cause the error as well?

– Jim Jimson
Nov 15 '18 at 11:26













@JimJimson Nope, the problem is the CONSTRAINTS on the table Professors. I'm quite sure about that.

– Kevin Costes
Nov 15 '18 at 11:28





@JimJimson Nope, the problem is the CONSTRAINTS on the table Professors. I'm quite sure about that.

– Kevin Costes
Nov 15 '18 at 11:28












1 Answer
1






active

oldest

votes


















0














It's a spelling mistake, your table is called Professor and your foreign key references Professors.



Try this:



CREATE TABLE Docencia (
hores_totals int,
Inicials char(2) not null,
Nivell varchar(10),
Curs varchar(10),
Lletra varchar(5),
CONSTRAINT profe FOREIGN KEY (Inicials) REFERENCES Professor (Inicials),
CONSTRAINT GrupNivell_pk FOREIGN KEY (Nivell, Curs, Lletra) REFERENCES Grup (Nivell, Curs, Lletra)
);





share|improve this answer























  • That was it, anyway, if I have to add a foreign key on the first table, do I have to use alter table?

    – Kevin Costes
    Nov 15 '18 at 11:39











  • Yes, unless you drop the table and recreate it. Obviously you'll lose any data in the table though.

    – Jim Jimson
    Nov 15 '18 at 11:46










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318407%2fhow-do-i-solve-error-cannot-add-foreign-key-constraint%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














It's a spelling mistake, your table is called Professor and your foreign key references Professors.



Try this:



CREATE TABLE Docencia (
hores_totals int,
Inicials char(2) not null,
Nivell varchar(10),
Curs varchar(10),
Lletra varchar(5),
CONSTRAINT profe FOREIGN KEY (Inicials) REFERENCES Professor (Inicials),
CONSTRAINT GrupNivell_pk FOREIGN KEY (Nivell, Curs, Lletra) REFERENCES Grup (Nivell, Curs, Lletra)
);





share|improve this answer























  • That was it, anyway, if I have to add a foreign key on the first table, do I have to use alter table?

    – Kevin Costes
    Nov 15 '18 at 11:39











  • Yes, unless you drop the table and recreate it. Obviously you'll lose any data in the table though.

    – Jim Jimson
    Nov 15 '18 at 11:46















0














It's a spelling mistake, your table is called Professor and your foreign key references Professors.



Try this:



CREATE TABLE Docencia (
hores_totals int,
Inicials char(2) not null,
Nivell varchar(10),
Curs varchar(10),
Lletra varchar(5),
CONSTRAINT profe FOREIGN KEY (Inicials) REFERENCES Professor (Inicials),
CONSTRAINT GrupNivell_pk FOREIGN KEY (Nivell, Curs, Lletra) REFERENCES Grup (Nivell, Curs, Lletra)
);





share|improve this answer























  • That was it, anyway, if I have to add a foreign key on the first table, do I have to use alter table?

    – Kevin Costes
    Nov 15 '18 at 11:39











  • Yes, unless you drop the table and recreate it. Obviously you'll lose any data in the table though.

    – Jim Jimson
    Nov 15 '18 at 11:46













0












0








0







It's a spelling mistake, your table is called Professor and your foreign key references Professors.



Try this:



CREATE TABLE Docencia (
hores_totals int,
Inicials char(2) not null,
Nivell varchar(10),
Curs varchar(10),
Lletra varchar(5),
CONSTRAINT profe FOREIGN KEY (Inicials) REFERENCES Professor (Inicials),
CONSTRAINT GrupNivell_pk FOREIGN KEY (Nivell, Curs, Lletra) REFERENCES Grup (Nivell, Curs, Lletra)
);





share|improve this answer













It's a spelling mistake, your table is called Professor and your foreign key references Professors.



Try this:



CREATE TABLE Docencia (
hores_totals int,
Inicials char(2) not null,
Nivell varchar(10),
Curs varchar(10),
Lletra varchar(5),
CONSTRAINT profe FOREIGN KEY (Inicials) REFERENCES Professor (Inicials),
CONSTRAINT GrupNivell_pk FOREIGN KEY (Nivell, Curs, Lletra) REFERENCES Grup (Nivell, Curs, Lletra)
);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 11:35









Jim JimsonJim Jimson

686414




686414












  • That was it, anyway, if I have to add a foreign key on the first table, do I have to use alter table?

    – Kevin Costes
    Nov 15 '18 at 11:39











  • Yes, unless you drop the table and recreate it. Obviously you'll lose any data in the table though.

    – Jim Jimson
    Nov 15 '18 at 11:46

















  • That was it, anyway, if I have to add a foreign key on the first table, do I have to use alter table?

    – Kevin Costes
    Nov 15 '18 at 11:39











  • Yes, unless you drop the table and recreate it. Obviously you'll lose any data in the table though.

    – Jim Jimson
    Nov 15 '18 at 11:46
















That was it, anyway, if I have to add a foreign key on the first table, do I have to use alter table?

– Kevin Costes
Nov 15 '18 at 11:39





That was it, anyway, if I have to add a foreign key on the first table, do I have to use alter table?

– Kevin Costes
Nov 15 '18 at 11:39













Yes, unless you drop the table and recreate it. Obviously you'll lose any data in the table though.

– Jim Jimson
Nov 15 '18 at 11:46





Yes, unless you drop the table and recreate it. Obviously you'll lose any data in the table though.

– Jim Jimson
Nov 15 '18 at 11:46



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318407%2fhow-do-i-solve-error-cannot-add-foreign-key-constraint%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto