SQL 在 sqlite3 中删除级联
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5890250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ON DELETE CASCADE in sqlite3
提问by LuckyLuke
I have the following structure: (Sorry for awkward names, it is because it is a sqlite database for my iPhone app which is not released yet)
我有以下结构:(对不起,名字笨拙,这是因为它是我的 iPhone 应用程序的 sqlite 数据库,尚未发布)
CREATE TABLE klb_log (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
log_comment varchar(512)
)
CREATE TABLE klb_log_food_maps (
uid integer,
did integer,
PRIMARY KEY (uid,did),
FOREIGN KEY (uid) references klb_log(id) ON DELETE CASCADE,
FOREIGN KEY (did) references klb_food(id) ON DELETE CASCADE
)
CREATE TABLE klb_food (
id integer,
description varchar(255),
PRIMARY KEY (id)
)
Is there a reason why the row in klb_log_food_maps
is not removed when I delete a row in klb_log
?
klb_log_food_maps
当我删除in 中的一行时,是否有理由不删除该行klb_log
?
回答by Paul Lefebvre
Foreign key support is not enabled in SQLite by default. You need to enable it manually each time you connect to the database using the pragma:
默认情况下,SQLite 中未启用外键支持。每次使用编译指示连接到数据库时,您都需要手动启用它:
PRAGMA foreign_keys = ON
回答by GKK
Do you have foreign key support enabled?
您是否启用了外键支持?
query PRAGMA foreign_keys = ON;
to turn it on
查询PRAGMA foreign_keys = ON;
以将其打开