SQLITE: Multiple foreign key referenced to multiple table cascade delete
在 SQLite 中,我想删除父表行,它必须删除所有相关的子表。我已经浏览了 StackExchange 和其他网站上的所有问题,但我的查询没有得到满意的地方。
我有四张桌子。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | TableA: id, primary key name TableB: id, primary key issues, tb_aid, #foreign key ref to TableA id tb_cid, #foreign key ref to TableC id tb_did, #foreign key ref to TableD id tb_eid, #foreign key ref to TableE id TableC: id, primary key column1, tb_bid, #foreign key ref to TABLE B id TableD: id, name TableE id, name |
我尝试了 JOIN,但不幸的是它在 SQLite 中不起作用。我不知道如何使用
您可以在这里使用级联删除。例如,对于
1 2 3 4 5 6 | CREATE TABLE TableC ( id INTEGER PRIMARY KEY AUTOINCREMENT, column1 TEXT, tb_bid INTEGER NOT NULL, CONSTRAINT fk_b FOREIGN KEY (tb_bid) REFERENCES TableB (id) ON DELETE CASCADE ) |
上表定义中约束的目的是将
您可以按如下方式定义您的
1 2 3 4 5 6 7 8 9 10 11 12 | CREATE TABLE TableB ( id INTEGER PRIMARY KEY AUTOINCREMENT, issues TEXT, tb_aid INTEGER NOT NULL, tb_cid INTEGER NOT NULL, tb_did INTEGER NOT NULL, tb_eid INTEGER NOT NULL, CONSTRAINT fk_a FOREIGN KEY (tb_aid) REFERENCES TableA (id) ON DELETE CASCADE, CONSTRAINT fk_c FOREIGN KEY (tb_cid) REFERENCES TableC (id) ON DELETE CASCADE, CONSTRAINT fk_d FOREIGN KEY (tb_did) REFERENCES TableD (id) ON DELETE CASCADE, CONSTRAINT fk_e FOREIGN KEY (tb_eid) REFERENCES TableE (id) ON DELETE CASCADE ) |