2015-09-04 58 views
0

我不斷收到以下錯誤在SQL代碼中找不到多個級聯路徑或循環的位置?

消息1785,級別16,狀態0,99號線 上表>「linkUserGroup」可能會導致循環或多個級聯路徑引進國外KEY約束「FK_linkUserGroup_tblStudent」。指定ON DELETE?> NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY約束。 消息1750,級別16,狀態0,行99 無法創建約束或索引。查看以前的錯誤。

我不明白如何循環或多個級聯路徑是可能的?我也無法理解FK對TeacherID的限制如何導致同樣的問題?我的理解是,如果要刪除父StudentID,那麼通過指定子表中的On Delete No Action應確保該記錄不會從子表中刪除。我不明白他們有什麼其他途徑或可能的循環?請幫助

CREATE TABLE tblUserAccount (
uaUserAccountID INT IDENTITY (1,1) CONSTRAINT PK_tblUserAccount PRIMARY KEY CLUSTERED, 
uaUserAccountTitle NVARCHAR (50) NOT NULL, 
uaUserAccountFirstName NVARCHAR (50) NOT NULL, 
uaUserAccountSurname NVARCHAR (50) NOT NULL, 
uaUserAccountUserName NVARCHAR (50) NOT NULL, 
uaUserAccountPassword NVARCHAR (50) NOT NULL, 
uaDateTimeModified DATETIME 
    CONSTRAINT DF_tblUserAccount_DateTimeModified DEFAULT SYSDATETIME() 
); 

CREATE TABLE tblRole (
rRoleID INT IDENTITY (1, 1) CONSTRAINT PK_tblRole PRIMARY KEY CLUSTERED, 
rRole NVARCHAR (50), 
rRoleDescription NVARCHAR (MAX) NULL, 
rDateTimeModified DATETIME 
    CONSTRAINT DF_tblRole_DateTimeModified DEFAULT SYSDATETIME() 
); 

CREATE TABLE linkUserAccountRole (
uarUserAccountID INT, 
uarRoleID INT, 
uarDateTimeModified DATETIME 
    CONSTRAINT DF_linkUserAccountRole_DateTimeModified DEFAULT SYSDATETIME() 
    ,CONSTRAINT PK_linkUserAcccountRole 
     PRIMARY KEY CLUSTERED (uarUserAccountID , uarRoleID) 
    ,CONSTRAINT FK_linkUserAcccountRole_tblRole 
     FOREIGN KEY (uarRoleID) 
     REFERENCES tblRole (rRoleID) 
     ON DELETE NO ACTION 
     ON UPDATE CASCADE 
    ,CONSTRAINT FK_linkUserAcccountRole_tblUserAccount 
     FOREIGN KEY (uarUserAccountID) 
     REFERENCES tblUserAccount (uaUserAccountID) 
     ON DELETE NO ACTION 
     ON UPDATE CASCADE 
); 

CREATE TABLE tblTeacher (
tTeacherID INT CONSTRAINT PK_tblTeacher PRIMARY KEY CLUSTERED, 
tUserAccountID INT, 
tDateTimeModified DATETIME 
    CONSTRAINT DF_tblTeacher_DateTimeModified DEFAULT SYSDATETIME(), 
    CONSTRAINT FK_tblTeacher_tblUserAccount 
     FOREIGN KEY (tUserAccountID) 
     REFERENCES tblUserAccount(uaUserAccountID) 
     ON DELETE CASCADE 
     ON UPDATE CASCADE 
); 

CREATE TABLE tblStudent (
sStudentID INT CONSTRAINT PK_tblStudent PRIMARY KEY CLUSTERED, 
sUserAccountID INT, 
sDateTimeModified DATETIME 
    CONSTRAINT DF_tblStudent_DateTimeModified DEFAULT SYSDATETIME(), 
    CONSTRAINT FK_tblStudent_tblUserAccount 
     FOREIGN KEY (sUserAccountID) 
     REFERENCES tblUserAccount(uaUserAccountID) 
     ON DELETE CASCADE 
     ON UPDATE CASCADE 

); 

CREATE TABLE tblKeyStage (
ksKeyStageGroupID INT IDENTITY (1,1) CONSTRAINT PK_tblKeyStage PRIMARY KEY CLUSTERED, 
ksKeyStageTitle NVARCHAR (50) NOT NULL, 
ksKeyStageDescription NVARCHAR(50), 
ksDateTimeModified DATETIME 
    CONSTRAINT DF_tblKeyStage_DateTimeModified DEFAULT SYSDATETIME() 
    ,CONSTRAINT UQ_tblKeyStage_KeyStageTitle UNIQUE (ksKeyStageTitle) 
); 

CREATE TABLE tblYearGroup (
ygYearGroupID INT IDENTITY (1,1) CONSTRAINT PK_tblYearGroup PRIMARY KEY CLUSTERED, 
ygYearGroupTitle NVARCHAR (50) NOT NULL, 
ygYearGroupDescription NVARCHAR(50), 
ygDateTimeModified DATETIME 
    CONSTRAINT DF_tblYearGroup_DateTimeModified DEFAULT SYSDATETIME() 
    ,CONSTRAINT UQ_tblYearGroup_YearGroupTitle UNIQUE (ygYearGroupTitle) 
); 

CREATE TABLE tblClassGroup (
cgClassGroupID INT IDENTITY (1,1) CONSTRAINT PK_tblClassGroup PRIMARY KEY CLUSTERED, 
cgClassGroupTitle NVARCHAR (50) NOT NULL, 
cgClassGroupDescription NVARCHAR (50), 
cgDateTimeModified DATETIME 
    CONSTRAINT DF_tblClassGroup_DateTimeModified DEFAULT SYSDATETIME() 
    ,CONSTRAINT UQ_tblClassGroup_ClassGroupTitle UNIQUE (cgClassGroupTitle) 
); 

CREATE TABLE tblLearningGroup (
lgLearningGroupID INT IDENTITY (1,1) CONSTRAINT PK_tblLearningGroup PRIMARY KEY CLUSTERED, 
lgLearningGroupTitle NVARCHAR (50) NOT NULL, 
lgLearningGroupDescription NVARCHAR (50), 
lgDateTimeModified DATETIME 
    CONSTRAINT DF_tblLearningGroup_DateTimeModified DEFAULT SYSDATETIME() 
    ,CONSTRAINT UQ_tblLearningGroup_LearningGroupTitle UNIQUE (lgLearningGroupTitle) 
); 

CREATE TABLE tblLearningGroup (
lgLearningGroupID INT IDENTITY (1,1) CONSTRAINT PK_tblLearningGroup PRIMARY KEY CLUSTERED, 
lgLearningGroupTitle NVARCHAR (50) NOT NULL, 
lgLearningGroupDescription NVARCHAR (50), 
lgDateTimeModified DATETIME 
    CONSTRAINT DF_tblLearningGroup_DateTimeModified DEFAULT SYSDATETIME() 
    ,CONSTRAINT UQ_tblLearningGroup_LearningGroupTitle UNIQUE (lgLearningGroupTitle) 
); 

CREATE TABLE linkUserGroup (
ugTeacherID INT, 
ugStudentID INT, 
ugKeyStageGroupID INT, 
ugYearGroupID INT, 
ugClassGroupID INT, 
ugLearningGroupID INT, 
ugDateTimeModified DATETIME 
    CONSTRAINT DF_linkUserGroup_DateTimeModified DEFAULT SYSDATETIME() 
    ,CONSTRAINT PK_linkUserGroup 
     PRIMARY KEY CLUSTERED (ugTeacherID, ugStudentID, ugClassGroupID, ugLearningGroupID) 
    ,CONSTRAINT FK_linkUserGroup_tblTeacher 
     FOREIGN KEY (ugTeacherID) 
     REFERENCES tblTeacher (tTeacherID) 
     ON DELETE NO ACTION 
     ON UPDATE CASCADE 
    ,CONSTRAINT FK_linkUserGroup_tblStudent 
     FOREIGN KEY (ugStudentID) 
     REFERENCES tblStudent (sStudentID) 
     ON DELETE NO ACTION 
     ON UPDATE CASCADE 
    ,CONSTRAINT FK_linkUserGroup_tblKeyStage 
     FOREIGN KEY (ugKeyStageGroupID) 
     REFERENCES tblKeyStage (ksKeyStageGroupID) 
     ON DELETE NO ACTION 
     ON UPDATE CASCADE 
    ,CONSTRAINT FK_linkUserGroup_tblYearGroup 
     FOREIGN KEY (ugYearGroupID) 
     REFERENCES tblYearGroup (ygYearGroupID) 
     ON DELETE NO ACTION 
     ON UPDATE CASCADE 
    ,CONSTRAINT FK_linkUserGroup_tblClassGroup 
     FOREIGN KEY (ugClassGroupID) 
     REFERENCES tblClassGroup (cgClassGroupID) 
     ON DELETE NO ACTION 
     ON UPDATE CASCADE 
    ,CONSTRAINT FK_linkUserGroup_tblLearningGroup 
     FOREIGN KEY (ugLearningGroupID) 
     REFERENCES tblLearningGroup (lgLearningGroupID) 
     ON DELETE NO ACTION 
     ON UPDATE CASCADE 
    ,CONSTRAINT UQ_linkUserGroup_PK_YearGroupID 
     UNIQUE (ugTeacherID, ugStudentID, ugClassGroupID, ugLearningGroupID, ugYearGroupID) 
);  

回答

0

多階路徑存在tblUserAccountlinkUserGroup之間:

tblUserAccount -> tblTeacher -> linkUserGroup 
tblUserAccount -> tblStudent -> linkUserGroup 

enter image description here

這不是FK約束本身,這是不允許的,但動作:ON UPDATE CASCADE

您可以爲FK_linkUserGroup_tblTeacher指定ON UPDATE CASCADE,或者爲FK_linkUserGroup_tblStudent指定ON UPDATE CASCADE,但兩者都不適用。

您的ON DELETE已被標記爲NO ACTION,所以沒關係。這是ON UPDATE,你應該決定該怎麼做。

有2個選項:

1)禁止更新其參與一個FK約束的關鍵。

爲此,請指定ON UPDATE NO ACTION,或者完全刪除ON UPDATE子句,因爲這是外鍵約束的默認行爲。

考慮到更新主鍵通常不是一個好主意(不管它是否被外鍵引用),我會推薦這個選項。

2)如果更改的sStudentIDtTeacherIDlinkUserGroup的自動傳播真的是你想要的,那麼你可以使用觸發器。

相關問題