有没有像“ON DELETE NULL”这样的东西?

时间:2016-02-03 11:27:57

标签: android sqlite cascading-deletes

我有两张桌子:

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";

2 个答案:

答案 0 :(得分:1)

您可以使用触发器来实现您的目标 在表上创建一个Before删除触发器,在该触发器中只需将forien键更新为null

//example
CREATE TRIGGER trg_delete BEFORE Delete 
ON categories 
BEGIN
update  notes set categories=null  where categories = old._id;
END

听到link了解有关触发器的更多信息

答案 1 :(得分:1)

在SQLite中,该操作不是拼写为“删除空”而是ON DELETE SET NULL

create table notes (
    [...],
    foreign key(category) references categories(title) on delete set null);