Search code examples
sqlentity-relationship

SQL database self interaction entity


I've been working on a database, wich is referent to an Aeroport management. I'm having a problem that it's freaking me out.. What i'm trying to do is, assuming that a client wants to know the distance between 2 locations, in miles or kms. As an example, if the user wants to know the distance between London and Amsterdam, should that distance be calculated by a formule or should it be already stored on the database?

        (1,N) ____________  
 ____________|__          |
|              |          |
| City/Airport |<---------|
|______________|

How can i show to user the distance between his 2 choices?

RicardoCosta


Solution

  • Your question isn't clear. I'm going to guess that this is a student/homework problem and that your database table looks like this:

    Airport1 Airport2 Distance
    

    If there's a direct route then there's a record whose airport1 value is London and whose airport2 value is Amsterdam: if that record exists, then the distance is the value in that record.

    If there's not a direct route then you need to find an indirect route: for example, a route with 1 stop-over, or 2 stop-overs, or etc.