Sqlite 外键不匹配?
Sqlite foreign key mismatch?
我已阅读 this 问题并理解引用的外键是唯一的,但不知何故插入 table 仍然抛出外键不匹配错误:
CREATE TABLE medication (
med_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
med_name VARCHAR (20) NOT NULL,
dosage VARCHAR (10)
);
CREATE TABLE disease (
dis_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
disease_name VARCHAR (20) NOT NULL
);
CREATE TABLE dis_med (
disease_id int NOT NULL,
medication_id int NOT NULL,
CONSTRAINT PK_dis_med PRIMARY KEY (disease_id, medication_id),
CONSTRAINT FK_dis FOREIGN KEY (disease_id) REFERENCES disease (dis_id),
CONSTRAINT FK_med FOREIGN KEY (medication_id) REFERENCES medication (med_id));
CREATE TABLE user_disease (
user_id REFERENCES user (user_id),
dis_id REFERENCES disease (dis_id),
med_id REFERENCES dis_med(medication_id),
CONSTRAINT PK_dis_user PRIMARY KEY (user_id, dis_id)
);
通过我引用问题中的列表:
- parent table(药物、疾病)存在。
- parent 列存在
- child table 引用了 parent table
中的所有主键列
更新1
我能够通过复合外键更改 user_disease table 来插入数据并绕过错误。如果有人能指出这里最好的设计是什么,我将不胜感激。非常感谢!
CREATE TABLE user_disease (
user_id REFERENCES user (user_id),
dis_id REFERENCES disease (dis_id),
med_id REFERENCES dis_med(medication_id),
CONSTRAINT FK_dis_med FOREIGN KEY REFERENCES dis_med(disease_id, medication_id),
CONSTRAINT PK_dis_user PRIMARY KEY (user_id, dis_id)
);
来自 SQLite Foreign Key Support/3. Required and Suggested Database Indexes:
Usually, the parent key of a foreign key constraint is the primary key of the parent table.
If they are not the primary key, then the parent key columns must be collectively
subject to a UNIQUE constraint or have a UNIQUE index.
有了这个:
CREATE TABLE user_disease (
...........................
med_id REFERENCES dis_med(medication_id),
...........................
);
user_disease
的 med_id
列引用了 dis_med
的 medication_id
列,它不是 dis_med
的 PRIMARY KEY
并且那里对它没有 UNIQUE
约束。它只是引用 med_id
of medication
.
为什么需要 user_disease
中的 med_id
列?
您有 dis_id
引用 disease
,它也可用于从 dis_med
(所有)检索来自 dis_med
的该疾病的行。
我已阅读 this 问题并理解引用的外键是唯一的,但不知何故插入 table 仍然抛出外键不匹配错误:
CREATE TABLE medication (
med_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
med_name VARCHAR (20) NOT NULL,
dosage VARCHAR (10)
);
CREATE TABLE disease (
dis_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
disease_name VARCHAR (20) NOT NULL
);
CREATE TABLE dis_med (
disease_id int NOT NULL,
medication_id int NOT NULL,
CONSTRAINT PK_dis_med PRIMARY KEY (disease_id, medication_id),
CONSTRAINT FK_dis FOREIGN KEY (disease_id) REFERENCES disease (dis_id),
CONSTRAINT FK_med FOREIGN KEY (medication_id) REFERENCES medication (med_id));
CREATE TABLE user_disease (
user_id REFERENCES user (user_id),
dis_id REFERENCES disease (dis_id),
med_id REFERENCES dis_med(medication_id),
CONSTRAINT PK_dis_user PRIMARY KEY (user_id, dis_id)
);
通过我引用问题中的列表:
- parent table(药物、疾病)存在。
- parent 列存在
- child table 引用了 parent table 中的所有主键列
更新1
我能够通过复合外键更改 user_disease table 来插入数据并绕过错误。如果有人能指出这里最好的设计是什么,我将不胜感激。非常感谢!
CREATE TABLE user_disease (
user_id REFERENCES user (user_id),
dis_id REFERENCES disease (dis_id),
med_id REFERENCES dis_med(medication_id),
CONSTRAINT FK_dis_med FOREIGN KEY REFERENCES dis_med(disease_id, medication_id),
CONSTRAINT PK_dis_user PRIMARY KEY (user_id, dis_id)
);
来自 SQLite Foreign Key Support/3. Required and Suggested Database Indexes:
Usually, the parent key of a foreign key constraint is the primary key of the parent table.
If they are not the primary key, then the parent key columns must be collectively subject to a UNIQUE constraint or have a UNIQUE index.
有了这个:
CREATE TABLE user_disease (
...........................
med_id REFERENCES dis_med(medication_id),
...........................
);
user_disease
的 med_id
列引用了 dis_med
的 medication_id
列,它不是 dis_med
的 PRIMARY KEY
并且那里对它没有 UNIQUE
约束。它只是引用 med_id
of medication
.
为什么需要 user_disease
中的 med_id
列?
您有 dis_id
引用 disease
,它也可用于从 dis_med
(所有)检索来自 dis_med
的该疾病的行。