【发布时间】:2016-05-12 13:47:56
【问题描述】:
我有两张桌子:
create table notes (
_id integer primary key autoincrement,
title text not null,
body text not null,
category text,
foreign key(category) references categories(title) on delete null);
create table categories (
_id integer primary key autoincrement,
title text unique not null);
我想知道是否有类似 ON DELETE CASCADE 的东西,但不是删除内容,而是将其更改为 NULL
显然ON DELETE NULL 不起作用。
编辑:我通过使用触发器实现了这一点:
"create trigger trg_delete before delete " +
"on categories " +
"begin " +
"update notes set category = null where category = old.title; " +
"end";
【问题讨论】:
-
是的,您可以使用 ON DELETE SET NULL .dev.mysql.com/doc/refman/5.7/en/ansi-diff-foreign-keys.html
-
@yasiriqbal776 它编译并且没有给出错误,但没有做任何事情:/
-
这不起作用,因为您必须将类别列设置为空,否则根据外键规则它不能为空
-
@yasiriqbal776 我不明白,我必须从类别中设置什么为空?你能举个例子吗?请注意,我不太喜欢数据库
-
创建表注释(_id整数主键自增,"+"标题文本不为空,正文不为空,类别文本默认为空,"+"外键(类别)引用类别(标题)删除设置默认值);试试这个查询我希望问题会得到解决:)
标签: android sqlite cascading-deletes