2012-08-05 8 views
0

J'ai une table product:Modélisation SQL (un à plusieurs) dans SQL Server 2005

ProdId(PK) 
Prod1 
Prod2 
Prod3 
Prod4  

et Certification table:

Certification(PK): 
Cert1 
Cert2 
Cert3 

Je dois modéliser ProdwithCert relation (pseudo tableau) comme suit :

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" 

Voici les contraintes:

  1. Impossible de dupliquer, par ex. dans le tableau ci-dessus, ProdwithCert6 - "Prod2 with Cert1, Cert2, Cert3" n'est pas permis
  2. Product a au moins un Certification

Comment puis-je modéliser correctement ProdwithCert relation dans SQL Server 2005?

Merci

Répondre

0

La première exigence peut être satisfaite par une table ProdwithCert avec une clé primaire (ou un index unique) sur (ProductId, CertId).

La deuxième exigence ne peut pas être appliquée à l'aide de contraintes de clé ou de vérification. La seule chose que je peux penser est de canaliser toutes les modifications à travers une procédure stockée. La procédure pourrait ajouter un produit avec un certificat dans une transaction.