If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > many to many relationships

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-02-05, 16:04
cwil2 cwil2 is offline
Registered User
 
Join Date: Jan 2005
Posts: 9
Exclamation many to many relationships

Hi i have a question regarding many to many relationships.

I am working on a c/work involving entities Airport, Aircraft and Flight. The flight has info including destination airport, departure airport and any stop over airports, date etc.

I have identified that the relationship between flight and airport is a many to many because the flight can have many stop over airports and the airport handles many flights.
BUT what about the attributes destination airport and departure airport i believe that these can only ever have one value i.e. one departure airport and one destination airport, but on the other hand it could be said that the flight has many airports??
does this mean if i decompose the many to many relationship that these fields (destination airport and departure airport)need to be added to the new entity??
Reply With Quote
  #2 (permalink)  
Old 01-02-05, 18:09
rajiravi rajiravi is offline
Registered User
 
Join Date: Apr 2004
Location: Toronto, Canada
Posts: 249
I think you are looking at the possibility of another entity called Flight Segments.
This could possibly have the attributes flight_id, flight_segment_number, departing_airport and destination.

The flight table would have attributes departure_airport and destination_airport, with the destination referring to the final destination of the flight.

So, you'd have the entities aircraft, airport, flight and flight_segment.

Ravi
Reply With Quote
  #3 (permalink)  
Old 01-03-05, 14:50
cwil2 cwil2 is offline
Registered User
 
Join Date: Jan 2005
Posts: 9
Thank's for your help
I had a simialr idea with introducing an entity route, but does this still not have the same problem as before where by the entity route then has a many to many relationship with airport? as the route will have two airports in the table?
I'm a bit unclear with many to many in this case as a flight only has one destination airport and one arrival airport but they are both airports so does this make it a many to many?

Another idea i had was to have an entity for departure airports and one for arrival airports these entities being made up of the flight_ID and the airport_ID from the respective entities. But this seems a bit far fetched to me? Also it would mean i would have to introduce a new entity to hold details of stopover airports of the journey?
How far sholud you decompose entites is wrong to crreate to many new entities?
Reply With Quote
  #4 (permalink)  
Old 01-03-05, 18:18
rajiravi rajiravi is offline
Registered User
 
Join Date: Apr 2004
Location: Toronto, Canada
Posts: 249
I do not see any problems.

Here are the four entities:

Aircraft: (aircraft_id, make, ....)

Airport: (Airport_code, city, country, ....)

Flight: (Flight_id, aircraft_id (FK to aircraft), departing_airport_code (FK to airport), final_destination_code (FK to airport))

Flight_Segment: (Flight_id, segment_number, departing_airport (FK to airport), arriving_airport (FK to airport))

So you do have a many to many relationship with airport. But then, so what?

Does this capture your needs? Is it in third normal form? If the answers to these questions is yes, go ahead and start coding.

If the answer is no, find out what requirements are missing.

Any more entities than these really do not seem to add anything to the model. Remember, the simplest design that meets the needs and that does not violate fundamentals is the best starting point.

Ravi
Reply With Quote
  #5 (permalink)  
Old 01-03-05, 23:31
MattR MattR is offline
Registered User
 
Join Date: Mar 2001
Location: Lexington, KY
Posts: 606
Isn't there a redundancy in the departing_airport_code and final_destination_code in the flight relation? The airport with segment = 1 is the starting of the trip and with the max segment is the destination.

Well, I guess removing the airports from the flight wouldn't handle diversions but for the vast majority of flights you’re going to store at least 2x the amount of data. Is this primarily for the ‘intended’ flight plan and not necessarily where the plane actually went? If not, you could have it so that the flight_segment table is filled in with intended airports/legs but then have another relation for, perhaps, unscheduled stops.

Also segment is useful to easily show the route, you could also calculate it where previous_destination = current airport (that is, you can make a closure over the airports to build the path).

Just some silly thoughts, yes.
__________________
Thanks,

Matt
Reply With Quote
  #6 (permalink)  
Old 01-03-05, 23:53
rajiravi rajiravi is offline
Registered User
 
Join Date: Apr 2004
Location: Toronto, Canada
Posts: 249
Matt,

I agree that there is redundancy in that the columns departing_airport_code and the final destination_code can be deduced from the flight segments. One could do away with them.

The other issue about closure, it seems to be similar to the problem of tracking history and contiguous dates. We want to be sure that the end-point of one segment (history or flight) is the starting point of the next. That is, they must be non-overlapping. There are many ways to handle this programmatically, but none declaratively that I know of.

For myself, I once wrote some code that would write the code for various triggers for overlapping time fields. Ought to be easy to generalize this to any other type of field, not just date-time fields.

The intended and actual flight plans are not always identical, of course. Then, depending upon what the business needs, one may choose to create a new entity for the actual flight. This need be populated only when there is deviation from the intended flight plan. A simple view would let the users/developers see the actual segments, whether these were according to original plan or differed from the intended.

Ravi
Reply With Quote
  #7 (permalink)  
Old 01-04-05, 03:56
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by rajiravi
We want to be sure that the end-point of one segment (history or flight) is the starting point of the next. That is, they must be non-overlapping. There are many ways to handle this programmatically, but none declaratively that I know of.
CHECK constraints
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 01-04-05, 05:03
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Quote:
Originally Posted by r937
CHECK constraints
Which DBMSs actually support check constraints that look at multiple rows together? I know they are part of the ANSI standard, but Oracle for example doesn't have them.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #9 (permalink)  
Old 01-04-05, 09:09
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
good question, tony

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #10 (permalink)  
Old 01-04-05, 09:13
MattR MattR is offline
Registered User
 
Join Date: Mar 2001
Location: Lexington, KY
Posts: 606
I think PostgreSQL's "Rule" system might be a form of table predicate, although I haven't looked at it too closely.
__________________
Thanks,

Matt
Reply With Quote
  #11 (permalink)  
Old 01-04-05, 13:59
cwil2 cwil2 is offline
Registered User
 
Join Date: Jan 2005
Posts: 9
Ravi

Thanks again for your input v helpful.
As regards to the many to many relationship in the books i have read and at uni we have been told to decompose any many to many relationships.
I dont understand the reasons why, other than MS access does not like them, but thats the way i must do it. Thanks again for your help it good to speak to those in the know. Cheers.
Reply With Quote
  #12 (permalink)  
Old 01-04-05, 15:33
rajiravi rajiravi is offline
Registered User
 
Join Date: Apr 2004
Location: Toronto, Canada
Posts: 249
To help understand why many-to-many relationships are often (almost always) decomposed using an intermediaate intersection entity, just try it out.

Design two versions of a very simple system, one in which the M:M relationship is not decomposed, and another in which it is decomposed.

See if you have any problems adding data, updating/deleting, etc. That will demonstrate why we must (or must not) decompose a M:M relationship.
Ravi
Reply With Quote
  #13 (permalink)  
Old 01-04-05, 22:19
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
The only database system that I can think of that handles many-to-many relationships well is Pick (and its derivatives like Revelation). None of the relational systems really like many-to-many relationships.

-PatP
Reply With Quote
  #14 (permalink)  
Old 01-05-05, 04:58
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Quote:
Originally Posted by Pat Phelan
The only database system that I can think of that handles many-to-many relationships well is Pick (and its derivatives like Revelation). None of the relational systems really like many-to-many relationships.
-PatP
What an extraordinary statement! In what sense do RDBMSs not handle many-to-many relationships well? Seems to me they do it extremely well.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #15 (permalink)  
Old 01-05-05, 08:17
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
perhaps what pat meant (and ravi, with his undecomposed relationship) is that an m-m relationship is not implemented directly by relational databases, but rather, is implemented as two 1-m relationships to a junction or association or linking table

try implementing a m-m with just two tables to see how difficult it is
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On