简体   繁体   English

避免数据库设计中列过多和复杂的最佳方法

[英]Best approach to avoid Too many columns and complexity in database design

Inventory Items : 库存物品:

Paper Size 
-----
A0
A1
A2 
etc

Paper Weight 
------------
80gsm
150gsm etc

Paper mode
----------
 Colour
 Bw

Paper type
-----------
 glass
 silk
 normal

Tabdividers and tabdivider Type
--------

Binding and Binding Types
--

Laminate and laminate Types
--

Such Inventory items and these all needs to be stored in invoice table 此类库存项目以及所有这些都需要存储在发票表中

How do you store them in Database using proper RDBMS. 如何使用适当的RDBMS将它们存储在数据库中。

As per my opinion for each list a master table and retrieval with JOINS. 根据我的意见,每个列表都包含一个主表并使用JOINS进行检索。 However this may be a little bit complex adding too many tables into the database. 但是,向数据库中添加太多表可能有点复杂。

This normalisation is having bit of problem when storing all this information against a Invoice. 当针对发票存储所有这些信息时,这种规范化存在一些问题。 This is causing too many columns in invoice table. 这导致发票表中的列过多。

Other way putting all of them into a one table with more columns and then each row will be a combination of them.. (hacking algorithm 4 list with 4 items over 24 records which will have reference ID). 另一种方法是将它们全部放入一个具有更多列的表中,然后每一行将它们组合在一起。(黑客算法4列表,其中24条记录中有4个项目将具有参考ID)。

Which one do you think the best and why!! 您认为哪一个最好?为什么!

Your initial idea is correct. 您最初的想法是正确的。 And anyone claiming that four tables is "a little bit complex" and/or "too many tables" shouldn't be doing database work. 任何声称四个表“有点复杂”和/或“太多表”的人都不应从事数据库工作。 This is what RDBMS's are designed (and tuned) to do. 这就是RDBMS设计(和调整)的目的。

Each of these 4 items is an individual property of something so they can't simply be put, as is, into a table that merges them. 这4个项目中的每一个都是某物的单独属性,因此不能简单地将它们原样放入合并它们的表中。 As you had thought, you start with: 如您所想,您首先要:

  • PaperSize 纸张尺寸
  • PaperWeight 纸张重量
  • PaperMode 纸张模式
  • PaperType 纸张类型

These are lookup tables and hence should have non-auto-incrementing ID fields. 这些是查找表,因此应具有非自动递增的ID字段。

These will be used as Foreign Key fields for the main paper-based entities. 这些将用作主要纸质实体的外键字段。

Or if they can only exist in certain combinations, then there would need to be a relationship table to capture/manage what those valid combinations are. 或者,如果它们只能以某些组合形式存在,那么就需要一个关系表来捕获/管理那些有效组合。 But those four paper "properties" would still be separate tables that Foreign Key to the relationship table. 但是,这四个纸质“属性”仍然是关系表的外键独立表。 Some people would put an separate ID field on that relationship table to uniquely identify the combination via a single value. 有些人会在该关系表上放置一个单独的ID字段,以通过单个值唯一地标识组合。 Personally, I wouldn't do that unless there was a technical requirement such as Replication (or some other process/feature) that required that each table had a single-field key. 就个人而言,除非有诸如复制(或其他过程/功能)之类的技术要求,要求每个表都具有一个单字段键,否则我不会这样做。 Instead, I would just make the PK out of the four ID fields that point to those paper "property" lookup tables. 取而代之的是,我只是从指向那些纸张“属性”查找表的四个ID字段中选出PK。 Then those four fields would still go into any paper-based entities. 然后,这四个字段仍将进入任何基于纸张的实体。 At that point the main paper entity tables would look about the same as they would if there wasn't the relationship table, the difference being that instead of having 4 FKs of a single ID field each, one to each of the paper "property" tables, there would be a single FK of 4 ID fields pointing back to the PK of the relationship table. 到那时,主要纸质实体表的外观将与没有关系表时的外观大致相同,不同之处在于,不是每个纸质“属性”都有4个FK,每个ID字段都是一个ID字段表中,将有一个FK,由4个ID字段指向关系表的PK。

Why not jam everything into a single table? 为什么不把所有东西都塞进一张桌子呢? Because: 因为:

  • It defeats the purpose of using a Relational Database Management System to flatten out the data into a non-relational structure. 它违反了使用关系数据库管理系统将数据展平为非关系结构的目的。
  • It is harder to grow that structure over time 随着时间的推移,发展这种结构变得更加困难
  • It makes finding all paper entities of a particular property clunkier 它使查找特定属性的所有纸质实体变得笨拙
  • It makes finding all paper entities of a particular property slower / less efficient 这会使查找特定属性的所有纸质实体变慢/效率降低
  • maybe other reasons? 也许还有其他原因?

EDIT: 编辑:
Regarding the new info (eg Invoice Table, etc) that wasn't in the question when I was writing the above, that should be abstracted via a Product/Inventory table that would capture these combinations. 关于我在撰写本文时没有出现的新信息(例如发票表等),应通过可捕获这些组合的产品/库存表进行抽象。 That is what I was referring to as the main paper entities. 这就是我所指的主要纸张实体。 The Invoice table would simply refer to a ProductID/InventoryID (just as an example) and the Product/Inventory table would have these paper property IDs. 发票表将仅引用ProductID / InventoryID(仅作为示例),而Product / Inventory表将具有这些纸张属性ID。 I don't see why these properties would be in an Invoice table. 我看不到为什么这些属性会出现在发票表中。

EDIT2: 编辑2:
Regarding the IDs of the "property" lookup tables, one reason that they should not be auto-incrementing is that their values should be taken from Enums in the app layer. 关于“属性”查找表的ID,不应对其进行自动递增的原因之一是,其值应取自应用程序层中的Enums。 These lookup tables are just a means of providing a "data dictionary" so that the database layer can have insight into what these values mean. 这些查询表只是提供“数据字典”的一种方式,以便数据库层可以洞悉这些值的含义。

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

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