0
我有表像梯級上刪除嵌套表
CREATE TABLE A (
Z INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (Z)
) ENGINE=InnoDB;
CREATE TABLE B (
Y INT NOT NULL,
Z INT NOT NULL,
PRIMARY KEY (Y , Z),
FOREIGN KEY (Z)
REFERENCES A (Z)
ON UPDATE CASCADE ON DELETE CASCADE
) ENGINE=InnoDB;
CREATE TABLE C (
Y INT NOT NULL,
Z INT NOT NULL,
PRIMARY KEY (Y , Z),
FOREIGN KEY (Y)
REFERENCES B (Y)
ON UPDATE CASCADE ON DELETE CASCADE,
FOREIGN KEY (Z)
REFERENCES B (Z)
ON UPDATE CASCADE ON DELETE CASCADE
) ENGINE=InnoDB;
INSERT INTO A VALUES (1);
INSERT INTO A VALUES (2);
INSERT INTO A VALUES (3);
INSERT INTO B VALUES (4, 2);
INSERT INTO B VALUES (3, 1);
INSERT INTO B VALUES (3, 3);
INSERT INTO B VALUES (4, 3);
INSERT INTO C VALUES (3, 1);
INSERT INTO C VALUES (4, 2);
如果我做正確
DELETE FROM A WHERE Z=3
B更新了和C沒有。
如果我做正確
DELETE FROM B WHERE Z=3
B更新了和C沒有。
級聯上的C刪除與任何事情完全無關的行。實際上,這些刪除命令都不應該連接到C.
爲什麼它會破壞C表?任何刪除都應該刪除表B中的(3,3)和(4,3)並停止。