为什么我在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 |
+--------------------------------------------------+
答案 0 :(得分:2)
默认情况下,每个documenation禁用外键约束。使用PRAGMA foreign_keys
命令启用:
sqlite> PRAGMA foreign_keys = ON;
答案 1 :(得分:1)