Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MySQL Error: Missing column for constraint in the referenced table

I’m a beginner and just started learning MySQL. I was trying to reference a foreign key to a certain table but i kept getting this error : (Error: Failed to add the foreign key constraint. Missing column ‘CNumber’ for constraint ‘exam_ibfk_1’ in the referenced table ‘course’)

CREATE TABLE Department (
    DCode INT PRIMARY KEY,
    DName VARCHAR(30),
    DContact INT,
    DCollege VARCHAR(30)
);

CREATE TABLE Course (
    CNmuber INT PRIMARY KEY,
    DCode INT,
    CName VARCHAR(30),
    CLevel INT,
    CDescription VARCHAR(200),
    FOREIGN KEY (DCode) REFERENCES Department(DCode)
);

CREATE TABLE Exam (
    ECode INT PRIMARY KEY,
    CNumber INT,
    Letter_Grade VARCHAR(1),
    Number_Grade INT,
    FOREIGN KEY (CNumber) REFERENCES Course(CNumber)
);

And by the way the first foreign key reference works just fine

FOREIGN KEY (DCode) REFERENCES Department(DCode)

I tried looking for answers on google and YouTube but I couldn’t find anything except for solutions that are way too complicated for my dumb brain!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Just read what you wrote carefully.

In the table "Course" you created a column "CNmuber" instead of "CNumber".

Then you tried to reference "Cnumber" that does not exist because the column is called "CNmuber"

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading