在 sqlite3 中删除级联 [英] ON DELETE CASCADE in sqlite3

查看:49
本文介绍了在 sqlite3 中删除级联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下结构:(对不起,名字有点尴尬,因为它是我的 iPhone 应用程序的 sqlite 数据库,尚未发布)

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)

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)
)

当我删除 klb_log 中的一行时,klb_log_food_maps 中的行没有被删除是否有原因?

Is there a reason why the row in klb_log_food_maps is not removed when I delete a row in klb_log?

推荐答案

SQLite 中默认不启用外键支持.每次使用编译指示连接到数据库时都需要手动启用它:

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:

PRAGMA foreign_keys = ON

这篇关于在 sqlite3 中删除级联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆