有没有类似 "ON DELETE NULL" 的东西?

Is there something like "ON DELETE NULL"?

我有两个表:

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

您可以使用触发器来实现您的预​​期 在 table 上创建删除前触发器,在该触发器中只需将 forien 键更新为 null

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

听说是link了解更多触发器

在 SQLite 中,该操作不是拼写为 ON DELETE NULL,而是 ON DELETE SET NULL:

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