1
首先,我創建了一個CourseSections表如下圖所示:SQL錯誤 - 有沒有主或候選鍵在引用表
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無一不精。誰能告訴我問題在哪裏?非常感謝!!
非常感謝你!有效!! – zoe