gpt4 book ai didi

sql - sqlite3中的复合外键麻烦

转载 作者:行者123 更新时间:2023-12-03 07:32:10 25 4
gpt4 key购买 nike

那里!我在sqlite3中的外键约束失败,而且我真的不知道为什么。我正在使用此处所述的复合外键(http://www.sqlite.org/foreignkeys.html#fk_composite)和“普通”外键。

这是我的架构:

sqlite> .schema sources
CREATE TABLE sources(
source_id VARCHAR(16) NOT NULL,
source_type INTEGER NOT NULL,
title VARCHAR(128) NOT NULL,
year INTEGER,
month INTEGER,
PRIMARY KEY(source_id),
UNIQUE(title)
);
sqlite> .schema author_aliases
CREATE TABLE author_aliases(
author_id INTEGER NOT NULL,
alias_id INTEGER NOT NULL,
forenames VARCHAR(128),
surname VARCHAR(128) NOT NULL,
PRIMARY KEY(author_id, alias_id),
FOREIGN KEY(author_id) REFERENCES authors(author_id),
UNIQUE(forenames, surname)
);
sqlite> .schema alias_source_relations
CREATE TABLE alias_source_relations(
source_id VARCHAR(16) NOT NULL,
author_id INTEGER NOT NULL,
alias_id INTEGER NOT NULL,
PRIMARY KEY(source_id, author_id, alias_id),
FOREIGN KEY(source_id) REFERENCES sources(source_id),
FOREIGN KEY(author_id, alias_id) REFERENCES author_aliases(author_id, alias_id)
);

这是外键引用的数据:
sqlite> SELECT * FROM sources WHERE source_id='Allen1980';
Allen1980|0|The definition of electronegativity and the chemistry of the noble gases|1980|
sqlite> SELECT * FROM author_aliases WHERE author_id=1 and alias_id=1;
1|1|Leland C.|Allen
sqlite> SELECT * FROM authors WHERE author_id=1;
1|Leland Cullen|Allen

这是我的插入:
sqlite> INSERT INTO alias_source_relations VALUES(1, 1, 'Allen1980');
Error: foreign key constraint failed

有人知道我在想什么吗?谢谢你的帮助!

问候,
玛丽安

最佳答案

检查列顺序!

INSERT INTO alias_source_relations VALUES('Allen1980', 1, 1);

关于sql - sqlite3中的复合外键麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19656431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com