sqlite – 主数据库中的外键引用附加数据库

前端之家收集整理的这篇文章主要介绍了sqlite – 主数据库中的外键引用附加数据库前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
sqlite3中是否有任何方法在主数据库中有一个引用附加数据库中的列的外键(反之亦然?)

我希望在多个进程之间共享附加的(只读)数据库,每个进程都有自己的(读/写)主数据库.

我像这样创建父表(在数据库’ParentDB’中):

create table Parent (id integer primary key);

现在我在主数据库中尝试这个:

attach 'parent.sqlite3' as ParentDB;
create table Child (id integer not null references Parent (id),constraint PK_Child primary key (id));
insert into ParentDB.Parent (id) values (42);

当我尝试它时,它创建没有错误的外键.现在我尝试在子表中插入一行:

insert into Child (id) values (42);

我收到这个错误

Error: no such table: main.Parent

所以它似乎总是假设父表和子表属于同一个数据库.

也是外键语法does not allow you to specify which database the parent table belongs to.

解决方法吗?

This question是相关的,但是这里父表和子表都在相同的附加数据库中,而我将它们放在不同的数据库中.

解决方法

sqlite的内置外键约束不适用于数据库.

唯一的解决方法是手动编写检查约束和执行相同检查的触发器.

猜你在找的Sqlite相关文章