世界!
当我在多对多关系中级联删除记录时,我遇到了孤儿。这是我的 sql 代码(用于 mysql):
DROP TABLE IF EXISTS skTable;
DROP TABLE IF EXISTS studentTable;
DROP TABLE IF EXISTS kTable;
DROP TABLE IF EXISTS classTable;
create table if not exists classTable (id int primary key) ENGINE=INNODB;
insert into classTable values(1);
insert into classTable values(2);
create table if not exists studentTable (id int primary key, classID int, CONSTRAINT FOREIGN KEY (classID) REFERENCES classTable(id) ON DELETE CASCADE) ENGINE=INNODB;
insert into studentTable values(1, 1);
insert into studentTable values(2, 2);
create table if not exists kTable (id int primary key) ENGINE=INNODB;
insert into kTable values(1);
insert into kTable values(2);
insert into kTable values(3);
create table if not exists skTable (id int primary key, studentID int, CONSTRAINT FOREIGN KEY(studentID) REFERENCES studentTable(id) on delete cascade, kID int, CONSTRAINT FOREIGN KEY(kID) REFERENCES kTable(id) on delete cascade) ENGINE=INNODB;
insert into skTable values(1,1,1);
insert into skTable values(2,2,2);
insert into skTable values(3,2,3);
insert into skTable values(4,1,2);
DELETE FROM classTable WHERE id=2;
我的期望是获得这些记录:
类表,1 条记录:
id=1
学生表,1 条记录:
id=1, classID=1
kTable,2条记录:
id=1
id=2
skTable,2 条记录:
id=1, studentID=1, kID=1
id=4, studentID=1, kID=2
问题是最后 kTable 有额外的记录
id=3
这是孤儿。
任何想法?谢谢。大号