SQLite和外键:文档中的错误示例不会失败

时间:2016-03-22 09:42:07

标签: sqlite

我正在查看https://sqlite.org/foreignkeys.html中的示例并尝试重现它们。这就是我得到的。

SQLite version 3.11.1 2016-03-03 16:17:53
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> PRAGMA foreign_keys=ON;
sqlite> PRAGMA foreign_keys; -- check that it registered
1
sqlite> CREATE TABLE parent(a PRIMARY KEY, b UNIQUE, c, d, e, f);
sqlite> CREATE UNIQUE INDEX i1 ON parent(c, d);
sqlite> CREATE INDEX i2 ON parent(e);
sqlite> CREATE UNIQUE INDEX i3 ON parent(f COLLATE nocase);
sqlite> CREATE TABLE child1(f, g REFERENCES parent(a));
sqlite> CREATE TABLE child2(h, i REFERENCES parent(b));
sqlite> CREATE TABLE child3(j, k, FOREIGN KEY(j, k) REFERENCES parent(c, d));
sqlite> CREATE TABLE child4(l, m REFERENCES parent(e)); -- Error?
sqlite> CREATE TABLE child5(n, o REFERENCES parent(f)); -- Error?
sqlite> CREATE TABLE child6(p, q, FOREIGN KEY(p, q) REFERENCES parent(b, c)); -- Error?
sqlite> CREATE TABLE child7(r REFERENCES parent(c)); -- Error?
sqlite> select * from sqlite_master where type='table';
table|parent|parent|2|CREATE TABLE parent(a PRIMARY KEY, b UNIQUE, c, d, e, f)
table|child1|child1|8|CREATE TABLE child1(f, g REFERENCES parent(a))
table|child2|child2|9|CREATE TABLE child2(h, i REFERENCES parent(b))
table|child3|child3|10|CREATE TABLE child3(j, k, FOREIGN KEY(j, k) REFERENCES parent(c, d))
table|child4|child4|11|CREATE TABLE child4(l, m REFERENCES parent(e))
table|child5|child5|12|CREATE TABLE child5(n, o REFERENCES parent(f))
table|child6|child6|13|CREATE TABLE child6(p, q, FOREIGN KEY(p, q) REFERENCES parent(b, c))
table|child7|child7|14|CREATE TABLE child7(r REFERENCES parent(c))

应该失败的示例(子4到7)不会,至少在创建表时。但是文档暗示它应该。我错过了什么?

1 个答案:

答案 0 :(得分:0)

documentation you linked to说:

  

如果数据库模式包含需要查找多个表定义以识别的外键错误,则在创建表时不会检测到这些错误。相反,此类错误会阻止应用程序准备以使用外键修改子表或父表的内容的SQL语句。