Android 是否有类似“的东西?”;在删除空时";?
我有两张桌子:Android 是否有类似“的东西?”;在删除空时";?,android,sqlite,cascading-deletes,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 int
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);
我想知道在DELETE CASCADE上是否有类似于的内容,但不是删除内容,而是将其更改为NULL
显然,删除NULL时的不起作用
编辑:我通过使用触发器实现了这一点:
"create trigger trg_delete before delete " +
"on categories " +
"begin " +
"update notes set category = null where category = old.title; " +
"end";
您可以使用触发器来实现预期目标
在表上创建一个Before delete触发器,并在该触发器中将forien键更新为null
//example
CREATE TRIGGER trg_delete BEFORE Delete
ON categories
BEGIN
update notes set categories=null where categories = old._id;
END
hear是了解SQLite中触发器的更多信息的一种方法,该操作不是在DELETE NULL上拼写的,而是:
是,您可以在删除集NULL上使用。@yasiriqbal776它编译并没有给出错误,但不做任何事情:/这不起作用,因为您必须将“类别”列设置为NULL,否则按外键规则它不能被删除null@yasiriqbal776我不明白这一点,我必须从类别中将什么设置为null?你能举个例子吗?注意,我对数据库创建表注释不太感兴趣(_idinteger主键自动递增,“+”标题文本不为null,正文文本不为null,类别文本默认为null,“+”外键(类别)在删除集默认值上引用类别(标题);试试这个查询,我希望问题会得到解决:)有没有办法在Android上做到这一点?是的,你可以在Android上做到这一点,也可以在创建表之后使用相同的查询。你也可以使用Sqlite管理器在那里做,自己试试
create table notes (
[...],
foreign key(category) references categories(title) on delete set null);