参照完整性在 N:N 关系中不起作用
这是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
N:M
关系中,N
或M
可以为零。参照完整性并未受到侵犯。删除班级将取消该班级所有学生的注册。同样,删除学生会将其从所有班级中展开。但删除学生永远不会导致级联取消班级,删除班级也不能开除学生。即使这是学生上的最后一堂课,你也会留下一个班级为零的学生,这在参照完整性规则下是有效的。
In an
N:M
relation, eitherN
orM
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.