简体   繁体   English

错误:外键引用来自两个不同表的主键

[英]Error: foreign key referring to primary keys from two different tables

I have created a test scenario like this: 我创建了这样的测试方案:

I have three tables namely father, child, food. 我有三个表,分别是父亲,孩子,食物。

Father table has primary auto increment key and a name column. 父表具有主自动递增键和名称列。 Child table has primary auto increment key and a name column. 子表具有主要的自动递增键和一个名称列。 Food has a column in which I want a foreign key and a column for food name. Food的一列中我需要一个外键,而食物名称则是一列。

father
-----------
id    name
1     kevin
2     adam

child
-----------
id    father_id    name
1     1            fred
2     1            john
3     2            alan

food
------------
person_id                name
1(from father table)     pizza
2(from child table)      burger
1(from child table)      hotdog

Now the food table has anomaly. 现在食物表出现异常。 I can't distinguish that (1 pizza) is for father or child... 我无法分辨(1个披萨)是给父亲还是给孩子的...

Test Cases: select food.name from food where father.id = 1; 测试用例:从食物中选择food.name,father.id = 1; OUTPUT: pizza select food.name from food where child.id = 1; 输出:披萨从child.id = 1的食物中选择food.name; OUTPUT: hotdog 输出:热狗

I believe that design is pretty bad. 我认为设计非常糟糕。 Why don't you create a "person" table, populate it and determine if a person is a child or father in it, then use person id in food table? 为什么不创建“人”表,填充它并确定其中是一个孩子还是父亲,然后在食物表中使用人ID?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM