General SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesGeneral SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 19th, 2003, 07:54 PM
marks marks is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 1 marks User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
complex relationships

I'm trying to model school courses and their prerequisites.

Originally I had a courses table, and a course_prereq table.
The course_prereq table had a course_id and prereq_course_id - just a simple many-to-many link for courses. For example, if course id=1 required courses 2 and 3, there'd be the following entries in the course_prereq table:

course_id, prereq_course_id
---------------------------------
1, 2
1, 3


This works fine if each course only had a plain list of requirements - but this isn't always the case.

A course may require, for example "3 and 4 and 5 and ((6 and 7) or (7 and 8 and 9))"

I'm completely at a loss as to how to represent these relationships in an SQL database. Any suggestions, or pointers in the right direction would be greatly appreciated.

Thanks.

Reply With Quote
  #2  
Old September 19th, 2003, 10:40 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
I'll try my best to explain this to you...

What you need is a "bridging" table...

for many-to-many relationships, you need an extra table to link them together...

an easier example would be using an address book...
you'll have a lot of people in your book, and the people may have more than one email address...

one way to set this up would be to have a PEOPLE table and an EMAIL table...

so lets say there's three people, we'll call them (1) Tom, (2) ****, and (3) Harry...

Tom will have two email addresses...
**** will have one email address...
Harry will have three email addresses...

we'll make the email address:
1 tom@home.com
2 tom@work.com
3 ****@work.com
4 harry@home.com
5 harry@work.com
6 harry@school.com


So as i said, you'll have two tables, PEOPLE an EMAIL respectively... they'll have ID numbers (as indicated above)..

now, to link the two tables together we'll use a bridging table... let's call it PEOPLE_EMAIL... it will hold the id numbers that connect the people with their respective emails... so the data would be represented like this:



PEOPLE, EMAIL

1, 1
1, 2
2, 3
3, 4
3, 5
3, 6



Perhaps this helps you out a bit?
it's a giant leap into database normalization...

Reply With Quote
  #3  
Old September 19th, 2003, 10:42 PM
Softwaremaker Softwaremaker is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: Elbonia with Dilbert and Wally
Posts: 4 Softwaremaker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi,

Dont overuse many-to-many relationships. If there is a need for many-to-many relationships, always create a lookup or index table which effectively links both many-to-many tables.

To use your example :
<Quote>
for example "3 and 4 and 5 and ((6 and 7) or (7 and 8 and 9))"
</Quote>

CourseID, CourseName
-----------------------------
1, Course1
2, Course2
3, Course3
4, Course4
5, Course5
6, Course6
7, Course7
8, Course8
9, Course9
10, Course10

ComplexPreReqCourseID,CommaDelimitedCourseID ==> Index Lookup Table
--------------------------------------------
1, (3)
2, (4)
3, (5)
4, (6,7)
5, (3,5)
6, (7,8,9)

MappingCourseTableID, CourseID, ComplexPreReqCourseID, OperatorType
--------------------------------------------------------------
1,1,1,"AND"
1,1,2,"AND"
1,1,3,"AND"
1,1,4,"AND"
1,1,6,"OR"

These are a bit raw at this moment and may not be elegant and not what you are looking for. But the idea is to create the Index Lookup Table. The values in this table must be predefined before mapping can take place.

hth

Last edited by Softwaremaker : September 19th, 2003 at 10:46 PM.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesGeneral SQL Development > complex relationships


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 6 hosted by Hostway