2017-08-02 167 views
-1

有人可以解釋爲什麼參考約束在以下SQLite會話似乎只是不工作?使用Windows 10的sqlite3這是一個真實的,但簡單的會話,您可以看到在會議上印刷SQLite的版本: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

它看起來並不像你啓用外鍵支持。 (默認關閉。)嘗試PRAGMA foreign_keys = 1;

P.S.將它放在一個文件~/.sqliterc中可以方便地自動爲sqlite3命令行工具打開它。