这是我的sql语句集:
char *sql = "BEGIN TRANSACTION;
CREATE TABLE Friends(id varchar UNIQUE);
CREATE TABLE Enemies(id2 varchar ,id3 INTEGER,CONSTRAINT id_econst
FOREIGN KEY(id2) REFERENCES Friends(id) ON DELETE CASCADE);
INSERT INTO Friends VALUES('11');
INSERT INTO Friends VALUES('2');
INSERT INTO Enemies VALUES('11',3);
INSERT INTO Enemies VALUES('3',5);
END TRANSACTION;
PRAGMA foreign_keys = ON;
DELETE FROM Friends WHERE id = '11';";
在“敌人”表上执行级联删除,而:
char *sql = "BEGIN TRANSACTION;
CREATE TABLE Friends(id varchar UNIQUE);
CREATE TABLE Enemies(id2 varchar ,id3 INTEGER,CONSTRAINT id_econst
FOREIGN KEY(id2) REFERENCES Friends(id) ON DELETE CASCADE);
INSERT INTO Friends VALUES('11');
INSERT INTO Friends VALUES('2');
INSERT INTO Enemies VALUES('11',3);
INSERT INTO Enemies VALUES('3',5);
PRAGMA foreign_keys = ON;
DELETE FROM Friends WHERE id = '11';
END TRANSACTION;";
正常删除。
为什么会这样呢?
最佳答案
您正在事务中启用外键支持。引用sqlite:
这种实用性是交易中的禁忌
https://sqlite.org/pragma.html#pragma_foreign_keys
在第二个示例中,编译指示无效,并且不强制使用外键约束。
关于sqlite - 何时在Sqlite中进行级联删除提交?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31532172/