Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Composite Key creation in SQL to refelct a n:m

Author  Topic 

gdanelian
Starting Member

1 Post

Posted - 2008-11-21 : 04:41:40
Hello all! My ER Diagram has a many to many relationship between MEMBERS and GOLF CLUB. Many members can join more than one golf club. Many GOLF CLUBS have many MEMBERS. When mapping the relations this means that I need a table to express this m:n relationships. I want to create a table in SQL with the table name: BELONGSTO and the two columns will be the primary key from MEMBERS (member-no) and the primary key from GOLF CLUB (club-no) to create a composite key (member-no, club-no). How would I do this in SQL?
ALL HELOP WELCOME, THANK YOU FOR YOUR TIME
G

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-21 : 04:47:08
If members can join more than one golfclub its better to kep a seperate identity column names belongs_to_id as primary key of relationship table belongs_to
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-11-21 : 04:55:24
something like
CREATE TABLE BELONGS_TO
(
Belongs_To_ID int identity(1,1) PRIMARY KEY CLUSTERED,
club_no int REFERENCES GOLF_CLUB (club_no),
member_no int REFERENCES MEMBERS (member_no)
)
Go to Top of Page

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2008-11-21 : 20:39:21
I would not have the identity and have the PK AS CLUB_NO and member_no.
If you do decide to have the ID then at least put a unique and not null constraint on the 2 columns
Go to Top of Page
   

- Advertisement -