Determining the proper primary key to foreign key setup

  • An exercise I was reading is about a bicycle shop.  There is a table called "Cycle" and a table called "Parts."  The question is asking, "what is the best foreign key for this relationship?"

    To answer this question, first I have to determine how these tables should be setup.  Since a cycle will have many unique parts, what is the best way to setup this relationship?  I have two pictures attached with the primary key table (at the top) pointing to the foreign key table (at the bottom), with the foreign key in red.  Between these two pictures, what is the proper way to setup these tables?

    I understand the idea behind these relationships but on this one I'm just not seeing it.  I think the solution they provided might be wrong.

    Attachments:
    You must be logged in to view attached files.
  • I imagine for this you'd need a third table, a junction table, since a cycle has many parts and each part can be used in many cycles.

    John

  • This example seems over-simplified, to the point of being impractical.

    To implement this in the real world, I'd be looking to develop a standard BOM model ... see here, for example.

    The absence of evidence is not evidence of absence.
    Martin Rees

    You can lead a horse to water, but a pencil must be lead.
    Stan Laurel

  • I think you need a new CycleParts table with two columns CycleId and  PartId. You need 2 foreign keys on this table. One pointing to the Cycle table the other to the Parts table. Your primary keys should be on CycleId on the Cycle table and PartId on the Parts table. The CycleParts table could have a compound primary key on columns (CycleId, PartId).

  • Jonathan AC Roberts wrote:

    I think you need a new CycleParts table with two columns CycleId and  PartId. You need 2 foreign keys on this table. One pointing to the Cycle table the other to the Parts table. Your primary keys should be on CycleId on the Cycle table and PartId on the Parts table. The CycleParts table could have a compound primary key on columns (CycleId, PartId).

    Thank you for your reply.  What you are saying makes sense.  Please see the new attached pic I created.  Is this what you were referring to?

    1. This means having a many to many relationship right?
    2. The two fields in this junction table in red are foreign key fields.  Can you really have 2 foreign key fields in a table?
    3. If I understand what you are saying, it sounds like your are saying that these two foreign key fields can also be a composite primary key.  Can you really have to foreign key fields serve as a primary key field in a table?

     

     

    Attachments:
    You must be logged in to view attached files.
    1. Yes
    2. Yes, and a lot more. On some Fact tables in data warehouses it's not uncommon to have over 100 foreign keys on a table. A foreign key must point to a primary key or a unique index on another table.
    3. Really it does little more than enforce uniqueness, so prevent duplicates. If you do not want to ensure uniqueness on those two columns, maybe the same part could be installed on different dates or two of the same parts are needed on a cycle, you could ensure uniqueness with a different way. Maybe by creating a unique constraint/index or primary key on CycleId, PartId and received date. But this depends on the specifications of the system.
  • Jonathan AC Roberts wrote:

     

      <li style="list-style-type: none;">

    1. Yes

     

      <li style="list-style-type: none;">

    1. Yes, and a lot more. On some Fact tables in data warehouses it's not uncommon to have over 100 foreign keys on a table. A foreign key must point to a primary key or a unique index on another table.

     

      <li style="list-style-type: none;">

    1. Really it does little more than enforce uniqueness, so prevent duplicates. If you do not want to ensure uniqueness on those two columns, maybe the same part could be installed on different dates or two of the same parts are needed on a cycle, you could ensure uniqueness with a different way. Maybe by creating a unique constraint/index or primary key on CycleId, PartId and received date. But this depends on the specifications of the system.

     

    Regarding question #3, let's say I didn't have columns called "Installed" and "Date Received."  I just put those in there to give examples of other fields.  So I I didn't have those fields, what would the answer to question 3 be?

  • michael.leach2015 wrote:

    Regarding question #3, let's say I didn't have columns called "Installed" and "Date Received."  I just put those in there to give examples of other fields.  So I I didn't have those fields, what would the answer to question 3 be?

    It's not uncommon to resolve a many to many relationship by adding a table with just two columns. Also, you really need to enforce uniqueness on this table. To do this you can make a compound primary key on the two columns or just add a unique constraint or index on them. It's also a good idea for every table to have a clustered index. The default clustering of a primary key is clustered.

Viewing 8 posts - 1 through 7 (of 7 total)

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