SQLite:级联删除不起作用

时间:2015-06-25 08:06:58

标签: android sqlite cascade delete-row sql-delete

我想在SQLite中使用外键(版本> 3.8)。

我创造了3个基地:

PRAGMA foreign_keys = ON;
CREATE TABLE `child1` (`child1_id` INTEGER PRIMARY KEY AUTOINCREMENT, `address` VARCHAR);

CREATE TABLE `child2` (`child2_id` INTEGER PRIMARY KEY AUTOINCREMENT, `country` VARCHAR);

CREATE TABLE `parent` (`parent_id` INTEGER PRIMARY KEY AUTOINCREMENT, 
    `name` VARCHAR, 
    `child1_fk` integer references child1 (child1_id) on delete cascade, 
    `child2_fk` integer references child2 (child2_id) on delete cascade);

然后注入一些数据

PRAGMA foreign_keys = ON;
INSERT INTO child1(address) VALUES('Street 1');
INSERT INTO child1(address) VALUES('Street 2');
INSERT INTO child2(country) VALUES('France');
INSERT INTO child2(country) VALUES('England');
INSERT INTO parent(name, child1_fk, child2_fk) VALUES('Smith', 1, 1);
INSERT INTO parent(name, child1_fk, child2_fk) VALUES('Dupond', 2, 2);

然后,我想删除父母,我希望这些孩子被删除:

PRAGMA foreign_keys = ON;
DELETE FROM parent WHERE parent_id=1;

但是id1的child1和child2并没有被删除......你知道为什么吗?

0 个答案:

没有答案