2012-08-06 106 views
-1

我有一个product表:SQL数据库设计咨询

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"

+0

这个问题应该移动到dba.stackexchange.com,因为它似乎是数据库架构相关。 psuedotable仅用于说明 – Gray 2014-03-30 17:12:25

回答

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 
+0

感谢Mike的帮助 – mxasim 2012-08-06 23:35:50

+0

没有问题@mxasim! – 2012-08-06 23:36:46

+0

MIke,架构不会阻止(product_group_certification)中的重复项。是否必须在“product_group_certification”上实施触发器以防止重复? – mxasim 2012-08-07 12:56:27

0

当您在上面说“psuedotable”时,是指所描述的表格仅用于说明,还是字面意思是您必须将文本字符串“Prod1 with Cert1”,“Prod1与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 
+0

。谢谢, – mxasim 2012-08-06 23:33:45