首先,我创建了一个 CourseSections 表,如下所示:
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)
);
然后我创建了另一个表,该表具有引用表 CourseSections 键的外键:
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)
);
然后我收到一条错误消息说
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'.
除了引用表 CourseSections 的外键之外,其余的外键都很好。谁能告诉我问题出在哪里?非常感谢!!