简体   繁体   English

SQL表设计-父表和子表中的FK相同

[英]SQL Table Design - Same FKs in Parent and Child tables

Columns in Member table: Member表中的列:

id           (PK)*
client_id    (FK)
location_id  (FK)
address
etc.

Columns in Member_Vehicle table: Member_Vehicle表中的列:

id           (PK)*
member_id    (FK)
mileage
etc.

Is it good design to replicate FKs already in the Member table into the Member_Vehicle table? Member表中已存在的FK复制到Member_Vehicle表中是否是一种好的设计?
So the New_Member_Vehicle table will be 因此, New_Member_Vehicle表将是

id           (PK)*
member_id    (FK)
client_id    (FK)
location_id  (FK)
mileage
etc.

Most of the processing in our application is around the Member_Vehicle table. 我们应用程序中的大多数处理都是围绕Member_Vehicle表进行的。
My thinking is - If the Member_Vehicle table readily has the IDs I need, then I can reduce the number of joins between Member and Member_Vehicle tables. 我的想法是-如果Member_Vehicle表很容易找到我需要的ID,那么我可以减少MemberMember_Vehicle表之间的联接数。

Your thoughts on why this 您对为什么这样的想法

  1. is acceptable 可以接受
    or 要么
  2. should not be done 不应该做

    are much appreciated. 非常感谢。 Thank you. 谢谢。

What you are considering is called denormalization and is generally a bad idea in a transaction processing system unless you have very good reasons to do it proven out by production performance issues, for example. 您所考虑的被称为非规范化 ,通常在事务处理系统中是个坏主意,除非您有充分的理由这样做(例如,由于生产性能问题证明)。

As a rule of thumb, your transaction processing system tables should be in third normal form (3NF) and you should back away from this only when needed. 根据经验,您的事务处理系统表应采用第三标准格式(3NF),并且仅在需要时才应使用此格式

The disadvantage to denormalization is that you could potentially introduce data anomalies (inconsistencies) if there are bugs in your code. 反规范化的缺点是,如果代码中存在错误,则可能会引入数据异常(不一致)。

Don't worry about joining tables. 不用担心联接表。 That is what relational database management systems are meant to do. 这就是关系数据库管理系统的作用。 Before denormalizing, I would consider other physical database performance tricks, like building a covering index . 在进行非规范化之前,我会考虑其他物理数据库性能技巧,例如构建覆盖索引

Can a client appear more than once in the Member table? 客户可以在“ Member表中多次出现吗? If not, the surrogate key there is superfluous. 如果不是,则那里的代理密钥是多余的。 In fact, I'll bet member_id is also a surrogate key. 实际上,我敢打赌member_id也是一个代理键。 Why would you generate another unique value to stand in for a value that is already unique? 为什么要生成另一个唯一值来代替已经唯一的值?

So if you drop the PK from the Member table, make client_id both the PK and FK and change member_id of Member_Vehicle to refer to 'client_id', you're problem is solved. 因此,如果您从Member表中删除PK,同时将client_id和PK都设置为FK,然后将Member_Vehicle member_id Member_Vehicle为引用“ client_id”,那么问题就解决了。

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

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