I have a product table:
ProdId(PK)
Prod1
Prod2
Prod3
Prod4
and Certification table:
Certification(PK):
Cert1
Cert2
Cert3
I need to model ProdwithCert relation (pseudo table) as follows:
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"
Following are the constraints:
- Cannot have duplicates, e.g. in above table,
ProdwithCert6 - "Prod2 with Cert1, Cert2, Cert3"is not allowed Producthas at least oneCertification
How do I properly model ProdwithCert relation in SQL Server 2005?
Thanks
The first requirement can be met by a
ProdwithCerttable with a primary key (or unique index) on(ProductId, CertId).The second requirement cannot be enforced using key or check constraints. The only thing I can think of is to channel all modifications through a stored procedure. The procedure could add a product with a certificate in one transaction.