我已经阅读了很多关于此的其他主题,但我仍然很难过。我创建了两个非常简单的表作为完整性检查,但无法让它们执行级联删除,因此此时需要一些帮助。
CREATE TABLE `test1` (
`test1_ID` int(11) NOT NULL AUTO_INCREMENT,
`test1_name` char(15) DEFAULT NULL,
PRIMARY KEY (`test1_ID`),
UNIQUE KEY `test1_ID_UNIQUE` (`test1_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `test2` (
`test2_ID` int(11) NOT NULL AUTO_INCREMENT,
`test2_FK_test1` int(11) NOT NULL,
`test2_name` char(15) DEFAULT NULL,
PRIMARY KEY (`test2_ID`),
UNIQUE KEY `test2_ID_UNIQUE` (`test2_ID`),
KEY `IDX_test2_FK_test1` (`test2_FK_test1`),
CONSTRAINT `FK_test2__test1` FOREIGN KEY (`test2_FK_test1`) REFERENCES `test1` (`test1_ID`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
用于测试的表格数据
Test 1 Table Data:
+----------+------------+
| test1_ID | test1_name |
+----------+------------+
| 1 | test1 r1 |
| 2 | test1 r2 |
+----------+------------+
Test 2 Table Data:
+----------+----------------+----------------+
| test2_ID | test2_FK_test1 | test2_name |
+----------+----------------+----------------+
| 1 | 1 | Test2 R1 - FK1 |
| 2 | 1 | Test2 R2 - FK1 |
| 3 | 1 | Test2 R3 - FK1 |
+----------+----------------+----------------+
Insert statements:
INSERT INTO `test1` VALUES (1,'test1 r2'),(2,'test1 r2');
INSERT INTO `test2` VALUES (1,1,'Test2 R1 - FK1'),(2,1,'Test2 R2 - FK1'),(3,1,'Test2 R3 - FK1');
如果我从 test1 表中删除第一行,则 test2 表数据没有任何反应
DELETE FROM test1 WHERE test1_ID = 1;