是的,有一種方法。請檢查mysql文檔以查找外鍵級聯刪除。
例子:
CREATE TABLE parent (id INT NOT NULL,
PRIMARY KEY (id)
) ENGINE=INNODB;
CREATE TABLE child (id INT, parent_id INT,
INDEX par_ind (parent_id),
FOREIGN KEY (parent_id) REFERENCES parent(id)
ON DELETE CASCADE
) ENGINE=INNODB;
Insert into parent set id=1;
Insert into parent set id=2;
Insert into parent set id=3;
Insert into child set id=1, parent_id=1;
Insert into child set id=2, parent_id=1;
select * from parent;
select * from child;
delete from parent where id=1;
select * from parent;
select * from child;
ID
1
2
3
Record Count: 3; Execution Time: 0ms View Execution Plan
ID PARENT_ID
1 1
2 1
Record Count: 2; Execution Time: 0ms View Execution Plan
Record Count: 0; Execution Time: 1ms
ID
2
3
Record Count: 2; Execution Time: 0ms View Execution Plan
Record Count: 0; Execution Time: 0ms
我會級聯 – sikas 2012-04-15 19:07:30
去如何使用級聯刪除?我可以從我的表中看到,我的cd_id是我的cds表中的主鍵。我想如果我刪除它,所有的子項也應該刪除? – 2012-04-15 19:11:49