【问题标题】:foreign key constraint in SQLite not working? [duplicate]SQLite 中的外键约束不起作用? [复制]
【发布时间】:2017-08-02 00:35:42
【问题描述】:

有人能解释一下为什么下面的 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>

【问题讨论】:

    标签: sqlite


    【解决方案1】:

    您似乎没有启用外键支持。 (默认关闭。)试试PRAGMA foreign_keys = 1;

    附:将其放入文件~/.sqliterc 以便为 sqlite3 命令行工具自动打开它很方便。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-17
      • 1970-01-01
      • 2013-05-03
      • 1970-01-01
      • 2022-12-22
      相关资源
      最近更新 更多