简体   繁体   English

可以将数据库中所有表的主键作为一个表的外键引用吗?

[英]Is it okay to reference the primary keys of all tables in database as a foreign key of one table?

I am trying to make this demo database where I have tables like 我正在尝试使这个演示数据库在其中有像

Household(Primary key - hid)
People(Primary key - pepid)
ParChildOld(Primary key - pcoid)
Job(Primary key - empid)
School(Primary key - schlid)
Pension(Primary key - penid)  

Now I have various attributes about details of people in each household in different tables. 现在,我在不同的表中具有关于每个家庭的人的详细信息的各种属性。

Can I take primary keys of all the tables and make one table, say AllKeys and reference them as foreign keys in this particular table? 我可以使用所有表的主键并创建一个表,例如说AllKeys并在此特定表中将它们作为外键引用吗?

ie: 即:

AllKeys Table
Primary Key: AKid
-- Other columns include 
foreign keys- hid,pepid,pcoid,empid,schid,penid*

I don't know if this is silly to ask or not, but, 我不知道这是否愚蠢,但是,

Is such a reference allowed? 是否允许这样的参考? Can it be considered as a normalized form? 可以将其视为规范化形式吗? Will the queries fired in this way joining AllKeys table and any other table(depending on query) work efficiently? 以这种方式触发的将AllKeys表和任何其他表(取决于查询)结合起来的查询能否有效地工作?

You could if all of those tables are related, but it would fail third and fourth normal forms, having both functional and multivalued dependencies in the table. 如果所有这些表都相关,则可以,但是它将失败第三和第四范式,因为表中具有功能性和多值依赖项。

Consider the huge amounts of duplication required to store relationships for just a single household: One household (hid=1), two parents (pepid=1,2), two children (pepid=3,4), four parent-child relationships (pcoid=1,2,3,4), two jobs (empid=1,2), one school for both children (schlid=1) and two pensions (penid=1,2). 考虑仅存储一个家庭的关系所需的大量重复:一个家庭(hid = 1),两个父母(pepid = 1,2),两个孩子(pepid = 3,4),四个亲子关系( pcoid = 1,2,3,4),两个工作(empid = 1,2),一个为两个孩子开设的学校(schlid = 1)和两个养老金(penid = 1,2)。

AKid | hid | pepid | pcoid | empid | schlid | penid
-----|-----|-------|-------|-------|--------|------
1    | 1   | 1     | 1     | 1     | null   | 1
2    | 1   | 1     | 2     | 1     | null   | 1
3    | 1   | 2     | 3     | 2     | null   | 2
4    | 1   | 2     | 4     | 2     | null   | 2
5    | 1   | 3     | 1     | null  | 1      | null
6    | 1   | 3     | 2     | null  | 1      | null
7    | 1   | 4     | 3     | null  | 1      | null
8    | 1   | 4     | 4     | null  | 1      | null

Another problem is that, assuming table ParChildOld contains a parent and a child column, you have no way of specifying whether for a pcoid reference in AllKeys, whether the pepid refers to the parent or the child. 另一个问题是,假设表ParChildOld包含一个父列和一个子列,则您无法指定是否在AllKeys中为仿形引用进行引用,而不指定pepid是引用父项还是子项。

In short, don't do it. 简而言之,不要这样做。

No, it will increase the complexities. 不,它将增加复杂性。

  1. You will need to write extra code to maintain these FKs 您将需要编写额外的代码来维护这些FK
  2. Due to extensive DML operations the table may create deadlocks (depending on what DB Engine you're going to use) and your whole DB will stop responding because most of the queries will be depending on this table 由于大量的DML操作,该表可能会创建死锁(取决于您将使用的数据库引擎),并且整个数据库将停止响应,因为大多数查询将取决于此表

Keep the data in normalized form and put your efforts on the queries and maintain it. 使数据保持规范化格式,并努力进行查询并进行维护。

You should be more focus on proper indexing on your tables (according to how you're going to query them). 您应该更加专注于对表进行正确的索引编制(根据查询方式)。

David Scarlett has defined it with a very nice example. David Scarlett用一个很好的例子定义了它。

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

相关问题 如何在作为主键和候选键的表中引用多个外键? - How to reference multiple foreign keys in a table that is a primary key and candidate keys? 一个表中的一个主键链接到两个不同表中的两个外键? - One primary key in one table linked to two foreign keys in two different tables? MySQL使用外键将数据插入表中,外键不是参考表的主键 - MySQL insert data into table with foreign keys that are no the primary key of the reference table 当主键在不同的表中时,可以引用两个外键吗? - Is it ok to reference two foreign keys when the primary key is in a different table? 一个外键引用了不同表的两个主键? - One foreign key referenced to two primary keys of different tables? 两个外键引用一个表和空的外键 - Two foreign keys reference one table and null able foreign key 不同表上多个主键的外键 - Foreign key to multiple primary keys on different tables 多个外键引用一个主键 - Multiple foreign keys referencing to one primary key 用主键和2个外键创建表并出错 - creating table with primary key and 2 foreign keys with error 可以将非顺序 ID 作为数据库中表的主键吗? - Is it okay to have non sequential ids as primary keys for a table in your database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM