所以我工作在我的項目,然後我試圖插入一些值是外鍵INSERT語句衝突與我的VB.net外鍵約束
INSERT語句衝突與外鍵約束條件 「SCHEDFK4」。衝突發生在數據庫「EnrollmentDatabase」中, 表「dbo.tblSubject」列'Subject_ID'。
這裏是我的SQL實體
CREATE TABLE tblGradeLevel
(
Grade_ID VARCHAR (5) NOT NULL,
Grade_Name VARCHAR (20) NULL,
PRIMARY KEY (Grade_ID)
)
CREATE TABLE tblSubject
(
Subject_ID VARCHAR (5)NOT NULL,
Subject_Name VARCHAR (20) NOT NULL,
Subject_Desc VARCHAR (20) NOT NULL,
Grade_Name VARCHAR (5) NOT NULL,
PRIMARY KEY (Subject_ID),
CONSTRAINT SUBFK1 FOREIGN KEY (Grade_ID)
REFERENCES tblGradeLevel (Grade_ID)
)
CREATE TABLE tblSchedule
(
Subject_Schedule_ID VARCHAR (5)NOT NULL,
Section_ID VARCHAR (5)NOT NULL,
Teacher_ID VARCHAR (5)NOT NULL,
Time_In VARCHAR (20)NOT NULL,
Time_Out VARCHAR (20)NOT NULL,
Subject_ID VARCHAR (5)NOT NULL,
Grade_ID VARCHAR (5)NOT NULL,
PRIMARY KEY (Subject_Schedule_ID),
CONSTRAINT SCHEDFK1 FOREIGN KEY (Student_ID)
REFERENCES tblStudent_Information (Student_ID),
CONSTRAINT SCHEDFK2 FOREIGN KEY (Section_ID)
REFERENCES tblSection (Section_ID),
CONSTRAINT SCHEDFK3 FOREIGN KEY (Teacher_ID)
REFERENCES tblTeacher (Teacher_ID),
CONSTRAINT SCHEDFK4 FOREIGN KEY (Subject_ID)
REFERENCES tblSubject (Subject_ID),
CONSTRAINT SCHEDFK5 FOREIGN KEY (Grade_ID)
REFERENCES tblGradeLevel (Grade_ID)
)
在dbo.tblsubject中檢查您的值。我認爲外鍵值(subjectid)不存在於dbo.tblsubject中,這就是發生錯誤的原因。 –