有人可以解释为什么以下SQLite会话中的引用约束似乎不起作用?这是在Windows 10上使用sqlite3的真实但简单的会话。您可以在会话中看到打印的SQLite版本:
C:\Users\johnd> sqlite3 -version
3.19.3 2017-06-08 14:26:16 0ee482a1e0eae22e08edc8978c9733a96603d4509645f348ebf55b579e89636b
C:\Users\johnd> sqlite3
SQLite version 3.19.3 2017-06-08 14:26:16
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> create table parent (
...> id integer primary key,
...> name text);
sqlite> create table child (
...> id integer primary key,
...> parent_id integer references parent(id),
...> description text);
sqlite> insert into parent (id, name) values(1, 'Bob');
sqlite> insert into parent (id, name) values(2, 'Sally');
sqlite> insert into child (id, parent_id, description) values(1, 3, 'bad ref');
sqlite> select * from parent;
1|Bob
2|Sally
sqlite> select * from child;
1|3|bad ref
sqlite> .exit
C:\Users\johnd>
答案 0 :(得分:1)
看起来您启用了外键支持。 (默认情况下已关闭。)尝试PRAGMA foreign_keys = 1;
。
P.S。将它放在文件~/.sqliterc
中以便为sqlite3命令行工具自动打开它是很方便的。