SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseSQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old August 17th, 2004, 10:23 AM
twopecker twopecker is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 148 twopecker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 48 m 59 sec
Reputation Power: 5
Help with setting up Many to Many relationship

I am pretty new to SQL, but having fun learning at this point. One of the things I am trying to do is setup an asp page here at work that list some of the clients, and the contacts for them.

Most of the sites have many contacts. A few of the contacts have many sites. How would I go about setting this up?

Currently, I have a table setup for sites and one for contacts. One the contacts table, I have a field for "site_id" and just relate it back for the site. This works fine to have multiple contacts associated to a site, but what do I do if the contact needs to be associated to more than one site?

Thanks in advance for the help,
Marcus

Reply With Quote
  #2  
Old August 17th, 2004, 10:34 AM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
ASP Free God 14th Plane (11500 - 11999 posts)
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,776 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 27 m 42 sec
Reputation Power: 470
You never want to have a many to many relationship.
You should create another table that has a relationship to both of them.

Reply With Quote
  #3  
Old August 17th, 2004, 12:11 PM
twopecker twopecker is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 148 twopecker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 48 m 59 sec
Reputation Power: 5
Could you please provide an example?

Reply With Quote
  #4  
Old August 17th, 2004, 02:45 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
ASP Free God 14th Plane (11500 - 11999 posts)
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,776 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 27 m 42 sec
Reputation Power: 470
Post some of your data and you database structure and I will be better able to demonstrate what I mean.

Reply With Quote
  #5  
Old August 17th, 2004, 03:46 PM
twopecker twopecker is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 148 twopecker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 48 m 59 sec
Reputation Power: 5
My contacts table consists of the a unique ID for the person, their name, phone #, email address, and the site_id they're associated with on the site table.

The site table consists just of the unique ID for each site, and the site name.

So for an example:

contacts
ID......Name..........phone............email...... ....site ID
1.......person1.......111-111........ 1@1.com ......1

site
site id....... site name
1..............site 1

sorry for all the periods, its the easiest way i could separate them to look like a table format.

Thanks,
Marcus

Reply With Quote
  #6  
Old August 17th, 2004, 06:13 PM
sbaxter sbaxter is offline
Moderator: Access, SQL
ASP Free God (5000 - 5499 posts)
 
Join Date: Oct 2003
Posts: 5,126 sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 1 h 2 m 51 sec
Reputation Power: 13
Table Contacts
ContactID (PK)

Table Site
SiteID (PK)

TableSiteContacts
ContactID (PK/FK from Contacts)
SiteID (PK/FK from Site)


This configuration allows one contact to be the contact for multiple sites and allows one site to have multiple contacts, Thus creating a Many to Many replationship between Contacts and sites.

Many to Many replationship exist only in theory. They can't exist physically (at least not yet)

S-

Reply With Quote
  #7  
Old August 18th, 2004, 09:21 AM
twopecker twopecker is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 148 twopecker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 48 m 59 sec
Reputation Power: 5
Thanks for the response...its a bit more clear now.

Im guessing on the SiteContacts table I would have a unique key, and use this to pull info with my SQL statement

Something like:

SELECT *
FROM contacts, site, contactsssite
WHERE contacts.ContactID = contactssite.ContactID
AND site.siteID = contactssite.siteID

....or am I way off here?

=)

Thanks,
Marcus

Reply With Quote
  #8  
Old August 18th, 2004, 03:03 PM
sbaxter sbaxter is offline
Moderator: Access, SQL
ASP Free God (5000 - 5499 posts)
 
Join Date: Oct 2003
Posts: 5,126 sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level)sbaxter User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 1 h 2 m 51 sec
Reputation Power: 13
Code:
SELECT Contacts.*, Site.*
FROM Site INNER JOIN (Contacts INNER JOIN COntactSite ON Contacts.COntactID = COntactSite.COntactID) ON Site.SiteID = COntactSite.SiteID;


S-

Reply With Quote
  #9  
Old August 19th, 2004, 08:55 AM
twopecker twopecker is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 148 twopecker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 48 m 59 sec
Reputation Power: 5
Thanks again.

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseSQL Development > Help with setting up Many to Many relationship


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT