how to join tables with key combinations? primary vs foreign

  • In SQL, how do I join tables with keys. I mean...can I do a join on the following..

    1. primary key to foreign key (I know this is a yes).
    2. primary key to primary key?
    3. foreign key to foreign?

    Is there a preferred method and does one display the data different from the other?

  • Think about relationships, Primary to Foreign is one to many, Primary to Primary is one to one, so think about what you need to join Foreign to Foreign?

    ...

  • adding to this . you do not even need to have a PK/FK and you can still join them.

    Although FK with indexes will help in some cases from a performance point of view, what really matters on those and other cases are the indexes and how the sql is built.

    As for data being different from one to the other - that will never be the case as long as the join clauses and the data contents are the same.

    If by asking "display data different from the other" is because you seen different behaviour on the order of records you then need to remember that unless you specify a order by there is no guarantee that the data will be "displayed" in the same order

  • thank you for the reply, so basically the answer is yes you can join PK to PK and FK to FK.

  • Joining FK to FK directly will give you unexpected results, a many to many relationship would normally require an intermediate table, nobody knows what you are modelling, I suggest you do some reading on database design.

    ...

  • A foreign key references a unique key.  It doesn't necessarily have to be the primary key.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply