无法找到SQL代码中多个级联路径或周期的位置?

时间:2015-09-04 15:30:54

标签: sql-server

我一直收到以下错误

  

Msg 1785,Level 16,State 0,Line 99   引入FOREIGN KEY约束' FK_linkUserGroup_tblStudent'在表格>' linkUserGroup'可能会导致循环或多个级联路径。指定ON DELETE?> NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。   Msg 1750,Level 16,State 0,Line 99   无法创建约束或索引。查看以前的错误。

我无法理解循环或多个级联路径是如何实现的?我也无法理解如何对TeacherID的FK约束不会导致同样的问题?我的理解是,如果要删除父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) 
);      

1 个答案:

答案 0 :(得分:0)

tblUserAccountlinkUserGroup之间存在多个级联路径:

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

enter image description here

不允许使用FK约束本身,但操作:ON UPDATE CASCADE

您可以为ON UPDATE CASCADE指定FK_linkUserGroup_tblTeacher,为ON UPDATE CASCADE指定FK_linkUserGroup_tblStudent,但不能为两者指定。

您的ON DELETE已标记为NO ACTION,所以没关系。 ON UPDATE你应该决定做什么。

有两个选项:

1)禁止更新参与FK约束的密钥。

为此,请指定ON UPDATE NO ACTION,或者只删除ON UPDATE子句,因为这是外键约束的默认行为。

考虑到更新主键通常不是一个好主意(无论是否由外键引用),我建议使用此选项。

2)如果您想要更改sStudentIDtTeacherIDlinkUserGroup的自动传播,那么您可以使用触发器。