0

这是一个简单的shema:

sqlite> .schema recordtypes
CREATE TABLE recordtypes (record_id text primary key);
sqlite> .schema headers
CREATE TABLE headers (header_id text primary key);
sqlite> .schema record_to_headers
CREATE TABLE record_to_headers (id INTEGER, recordid TEXT, headerid TEXT, FOREIGN KEY(recordid) REFERENCES recordtypes(record_id), FOREIGN KEY(headerid) REFERENCES headers(header_id));

然后我将值插入然后recordtypes插入record_to_headers

sqlite> insert into recordtypes values("test");
sqlite> insert into record_to_headers values (1, "test", "yeah");

然后我从表中删除“测试”,recordtypes期望它也从record_to_headers表中删除,但这永远不会发生:

sqlite> delete from recordtypes where record_id = "test";
sqlite> select * from recordtypes;
sqlite> select * from record_to_headers;
1|test|yeah

如果我从表中删除值,我需要行为,recordtypes它也会从record_to_header表中删除。请建议如何这样做。非常感谢。

4

1 回答 1

2

SQLLite 的经验并不多,但从我可以看到的文档中,SQLLite 支持外键级联。当父行被删除时,这将删除子行:

FOREIGN KEY(headerid) REFERENCES headers(header_id) ON DELETE CASCADE

您应该查阅文档以查看是否需要预先在 SQLLite 中进行配置。

于 2013-07-25T12:38:14.557 回答