SQL数据库设计建议

时间:2012-08-06 22:57:47

标签: sql database-design

我有一个product表:

ProdId(PK)
Prod1
Prod2
Prod3 
Prod4    

Certification表:

Certification(PK):
Cert1
Cert2
Cert3

如何建模以下关系(伪表):

ProdwithCertId(PK)              ProdwithCert
ProdwithCert1                   "Prod1 with Cert1"
ProdwithCert2                   "Prod1 with Cert1, Cert2"
ProdwithCert3                   "Prod1 with Cert1, Cert2, Cert3"
ProdwithCert4                   "Prod2 with Cert1, Cert2"
ProdwithCert5                   "Prod2 with Cert1, Cert2, Cert3"

不能有重复项,例如在上表中,不允许ProdwithCert6 - "Prod2 with Cert1, Cert2, Cert3"

2 个答案:

答案 0 :(得分:1)

这是架构。

table product (id, name)
table certification (id, name)
table product_group (id, product_id)
table product_group_certification (id, product_group_id, certification_id)

现在,我们假设我们在上面的示例中使用了Prod2,在此架构中看起来像这样。

**product**
1, Prod2

**certification**
1, Cert1
2, Cert2
3, Cert3

**product_group**
1, 1  // Prod2 with Cert1, Cert2
2, 1  // Prod2 with Cert1, Cert2, Cert3

**product_group_certification**
1, 1, 1
2, 1, 2
3, 2, 1
4, 2, 2
5, 2, 3

答案 1 :(得分:0)

当您在上面说“psuedotable”时,您是否意味着所描述的表格只是说明性的,或者您的字面意思是您必须存储文本字符串“Prod1 with Cert1”,“Prod1 with Cert1,Cert2”等等?如果它是前者,那么对我来说似乎是基本的解决方案类似于以下内容:

ProdWithCertId (PK)    Prod (SK) Cert (SK)
1                      Prod1     Cert1
2                      Prod1     Cert2
3                      Prod1     Cert3
4                      Prod2     Cert1
5                      Prod2     Cert2
6                      Prod2     Cert3