Search code examples
androidsqlitecascading-deletes

Is there something like "ON DELETE NULL"?


I have two tables:

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

I'm wondering if there's something like ON DELETE CASCADE but instead of deleting the content, changing it to NULL

Obviously ON DELETE NULL does not work.

EDIT: I achieved this by using a trigger:

"create trigger trg_delete before delete " +
                "on categories " +
                "begin " +
                "update notes set category = null where category = old.title; " +
                "end";

Solution

  • you can use triggers to achieve what you are intended Create a Before delete trigger on your table and in that trigger just update the forien key to null

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

    hear is a link to know more about triggers