Firstly I created a CourseSections table like below:
Create Table CourseSections(
CourseNo Integer NOT NULL CHECK(CourseNo BETWEEN 100 AND 999),
SectionNo Integer NOT NULL,
InstructorNo Integer NOT NULL,
Year Integer NOT NULL,
Semester Integer NOT NULL,
FOREIGN KEY(CourseNo) REFERENCES Courses(CourseNo),
PRIMARY KEY(SectionNo, Year, Semester)
);
Then I created another table that has foreign keys referencing to table CourseSections keys:
Create Table Enrollments(
CourseNo Integer NOT NULL CHECK(CourseNo BETWEEN 100 AND 999),
Semester Integer NOT NULL,
Year Integer NOT NULL,
SectionNo Integer NOT NULL,
FOREIGN KEY(CourseNo) REFERENCES Courses(CourseNo),
FOREIGN KEY(Year) REFERENCES CourseSections(Year),
FOREIGN KEY(Semester) REFERENCES CourseSections(Semester),
FOREIGN KEY(SectionNo) REFERENCES CourseSections(SectionNo)
);
Then I’m getting an error message saying
There are no primary or candidate keys in the referenced table 'CourseSections'
that match the referencing column list in the foreign key
FK__Enrollment__Year__3890146B'.
The rest foreign keys are all fine except the ones referencing to the table CourseSections. Can anyone tell me where the problem is? Very much appreciated!!
should work, but the other foreign keys will not since they don’t really have an index; they are lower parts of a composite index. A composite index is used for columns only in order of how it was created. You would either needs to make unique indexes on CourseSections.Year and CourseSections.Semester, or, more likely, create a single composite foreign key like this: