0

为什么我最后一次插入Printer表中没有返回错误?我认为(希望)由于参照完整性而被拒绝。

代码

CREATE TABLE IF NOT EXISTS
Client (
    hostName    TEXT PRIMARY KEY,
    MACAddress  TEXT NOT NULL,
    deviceType  TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS
Printer (
    hostName    TEXT NOT NULL REFERENCES Client(hostName),
    outputType  TEXT NOT NULL,
    printerName TEXT NOT NULL,
    PRIMARY KEY (hostname, outputType),
    FOREIGN KEY (hostName) REFERENCES Client(hostName)
      ON DELETE CASCADE
      ON UPDATE CASCADE
);

insert into Client (hostName, MACAddress, deviceType)
values ('billlaptop.private.ycc', 'bc:ea:c5:13:b3:09','Client');

insert into Printer (hostName, outputType, printerName)
values ('billlaptop.private.ycc', 'Receipt', 'Thermal');

insert into Printer (hostName, outputType, printerName)
values ('xxxx.private.ycc', 'Receipt', 'Thermal');

select * from Printer;

输出

+--------------------------------------------------+
| hostName                outputType   printerName |
+--------------------------------------------------+
| billlaptop.private.ycc  Receipt      Thermal     |
| xxxx.private.ycc        Receipt      Thermal     |
+--------------------------------------------------+
4

2 回答 2

2

每个文档默认禁用外键约束。使用PRAGMA foreign_keys命令启用:

sqlite> PRAGMA foreign_keys = ON;

摆弄预期的错误。

于 2013-09-06T18:02:55.833 回答
1

根据SQLite3 pragma page,从 SQLite 版本 3.6.19 开始,外键强制执行的默认设置为 OFF。

您必须发表声明

PRAGMA foreign_keys = ON;

启用检查。

于 2013-09-06T18:02:48.060 回答