根据this允许使用null外键,除非并且直到我们向模式添加适当的“NOT NULL”约束。
但我看到了一些不同的行为,
sqlite> PRAGMA Foreign_keys;
1
sqlite> create table proc (pid integer, name text, ppid integer, foreign key (ppid)
references proc (id));
sqlite> .schema proc
CREATE TABLE proc (pid integer, name text, ppid integer, foreign key (ppid) references
proc (id));
sqlite> insert into proc (pid, name, ppid) values (0, "init", null);
Error: foreign key mismatch
sqlite> PRAGMA Foreign_keys=OFF;
sqlite> PRAGMA Foreign_keys;
0
sqlite> insert into proc (pid, name, ppid) values (0, "init", null)
sqlite> select * from proc;
0|init|
当PRAGMA foreign_keys = ON时,如何在sqlite中允许空外键?或者根本不可能?
答案 0 :(得分:1)
pid
,而不是id
。UNIQUE
或PRIMARY KEY
约束。答案 1 :(得分:0)
尝试通过将表创建语句更改为:{/ p>来添加foreign key clause
CREATE TABLE proc (pid integer, name text, ppid integer, foreign key (ppid) references
proc (id) ON UPDATE CASCADE ON DELETE SET NULL);