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!
>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"