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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 10:25:18  来源:igfitidea点击:

ON DELETE CASCADE in sqlite3

sqlsqlite

提问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_mapsis 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;以将其打开