参照完整性在 N:N 关系中不起作用

发布于 2024-12-07 20:48:06 字数 1258 浏览 0 评论 0原文

这是 ms 创建表脚本:

SchoolclassCode 和 Pupil 表之间是 N:M 关系

CREATE TABLE Schoolclasscode (
  schoolclassId    integer PRIMARY KEY AUTOINCREMENT NOT NULL 
);

CREATE TABLE SchoolclasscodePupil (
  pupilId_FK        integer NOT NULL,
  schoolclassId_FK  integer NOT NULL,
  /* Foreign keys */
  FOREIGN KEY (schoolclassId_FK)
    REFERENCES Schoolclasscode(schoolclassId)
    ON DELETE CASCADE
    ON UPDATE NO ACTION, 
  FOREIGN KEY (pupilId_FK)
    REFERENCES pupil(pupilId)
    ON DELETE CASCADE
    ON UPDATE NO ACTION
);

CREATE TABLE pupil (
  pupilId           integer PRIMARY KEY AUTOINCREMENT NOT NULL  
);

当我在代码中删除 SchoolclassCode 对象时:

public void DeleteSchoolclass(int schoolclassCodeID, SQLiteConnection con)
        {
            using (SQLiteCommand com = new SQLiteCommand(con))
            {
                com.CommandText = "DELETE FROM schoolclasscode WHERE SchoolclassId = @SchoolclassId";
                com.Parameters.Add(new SQLiteParameter("@SchoolclassId", schoolclassCodeID));
                com.ExecuteNonQuery(); 
            }
        }

schoolclasscode 表中的条目被删除。但仅此而已。我什至可以另外删除 SchoolclasscodePupil 中的 schoolclasscodeId_FK,但级联删除约束不会删除任何学生。

我有什么错吗?

This is ms create table script:

It is a N:M relation between the SchoolclassCode and the Pupil table

CREATE TABLE Schoolclasscode (
  schoolclassId    integer PRIMARY KEY AUTOINCREMENT NOT NULL 
);

CREATE TABLE SchoolclasscodePupil (
  pupilId_FK        integer NOT NULL,
  schoolclassId_FK  integer NOT NULL,
  /* Foreign keys */
  FOREIGN KEY (schoolclassId_FK)
    REFERENCES Schoolclasscode(schoolclassId)
    ON DELETE CASCADE
    ON UPDATE NO ACTION, 
  FOREIGN KEY (pupilId_FK)
    REFERENCES pupil(pupilId)
    ON DELETE CASCADE
    ON UPDATE NO ACTION
);

CREATE TABLE pupil (
  pupilId           integer PRIMARY KEY AUTOINCREMENT NOT NULL  
);

When I delete a SchoolclassCode object in my code:

public void DeleteSchoolclass(int schoolclassCodeID, SQLiteConnection con)
        {
            using (SQLiteCommand com = new SQLiteCommand(con))
            {
                com.CommandText = "DELETE FROM schoolclasscode WHERE SchoolclassId = @SchoolclassId";
                com.Parameters.Add(new SQLiteParameter("@SchoolclassId", schoolclassCodeID));
                com.ExecuteNonQuery(); 
            }
        }

The entry in the schoolclasscode table is deleted. But nothing more. I can even additionally delete the schoolclasscodeId_FK in the SchoolclasscodePupil but no pupils were deleted by a cascade delete constraint.

What do I wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

_蜘蛛 2024-12-14 20:48:06

N:M 关系中,NM 可以为零。参照完整性并未受到侵犯。

删除班级将取消该班级所有学生的注册。同样,删除学生会将其从所有班级中展开。但删除学生永远不会导致级联取消班级,删除班级也不能开除学生。即使这是学生上的最后一堂课,你也会留下一个班级为零的学生,这在参照完整性规则下是有效的。

In an N:M relation, either N or M may be zero. Referential integrity has not been violated.

Deleting a class will deregister all pupils from that class. Similarly deleting a pupil will unroll them from all classes. But deleting a pupil can never cause a cascade to cancel a class, nor can deleting a class expel a pupil. Even if it's the last class the pupil was taking, you're left with a pupil who has zero classes, which is valid under the referential integrity rules.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文