简体   繁体   English

规范化此数据的最佳方法

[英]Best way to normalize this data

I have two tables, 'Product' and 'Product Packs'. 我有两个表,'产品'和'产品包'。

One product can appear in many product packs and a product pack can have many products (A many to many relationship). 一种产品可以出现在许多产品包中,而产品包可以有许多产品(多对多关系)。

I have also created a linking table to try and resolve this relationship but I am left with the following conundrum: 我还创建了一个链接表来尝试解决这种关系,但我留下了以下难题:

-------------------
|  Linking table  |
-------------------
| Prod_Id | PP_id |
|  1      |  3    |
|  1      |  4    |
|  1      |  5    |
|  1      |  6    |
|  1      |  7    |
|  2      |  5    |
|  2      |  7    |
|  2      |  8    |
|  2      |  10   |
|  2      |  4    |

Is this normal practice for database design? 这是数据库设计的常规做法吗? Could this be refined further? 这可以进一步改进吗?

You have a good starting point. 你有一个很好的起点。

Taking it here you should consider, making the two fields of the table into a composite primay key. 在这里你应该考虑,使表的两个字段成为复合的主键。 That would prevent duplicate records as noted by @musical_coder. 这将防止@musical_coder注意到的重复记录。

You might also consider adding an integer column that indicates the quantity of products in the package. 您还可以考虑添加一个整数列,指示包中的产品数量。

Finally, you might want to add some metadata columns such as CreatedWhen, CreatedBy, LastUpdatedWhen, and LastUpdatedBy. 最后,您可能希望添加一些元数据列,例如CreatedWhen,CreatedBy,LastUpdatedWhen和LastUpdatedBy。 These tend to come in handy from time to time. 这些往往会不时派上用场。

In my experience I would say that's no problem with this kind of relationship, but you need to be very careful when analysing data , but how you model is logical that a product is in a package and a package contains n products. 根据我的经验,我会说这种关系没有问题,但在分析数据时需要非常小心,但是如何建模是合乎逻辑的,产品在包装中,包装包含n种产品。 So that's not bad. 所以这还不错。

Edit: This is an obsolete answer since the poster fixed his example data to not contain duplicate tuples anymore 编辑:这是一个过时的答案,因为海报修复了他的示例数据不再包含重复的元组

From a database normalization perspective, this design is a bit tricky: If having two identical tuples in that table is supposed to have a meaning (eg, a count), then this table has a multi-set semantics, which does not mix well with the relational model, since you don't have a key. 从数据库规范化的角度来看,这个设计有点棘手:如果在该表中有两个相同的元组应该有意义(例如,一个计数),那么这个表有一个多集语义,它不能很好地与关系模型,因为你没有密钥。

Having a table (Prod_ID, PP_Id, Count) with primary key (Prod_ID,PP_Id) is be a better (third normal form) design. 具有主键(Prod_ID,PP_Id)的表(Prod_ID,PP_Id,Count)是更好的(第三范式)设计。

Edit: 编辑:

So your table becomes 所以你的桌子变成了

create table t (Prod_Id int, PP_Id int, Count int, primary key(Prod_Id,PP_Id));

insert into t values 
(1,3,2),
(1,4,1),
(1,5,1),
(1,6,1),
(1,7,1),
(2,5,1),
(2,7,1),
(2,8,1),
(2,10,1),
(2,4,1);

select * from t;

Prod_Id     PP_Id       Count
----------  ----------  ----------
1           3           2
1           4           1
1           5           1
1           6           1
1           7           1
2           5           1
2           7           1
2           8           1
2           10          1
2           4           1

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

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