Results 1 to 3 of 3

Thread: Terribly lost please help

  1. #1
    Join Date
    Oct 2010
    Posts
    2

    Terribly lost please help

    I am working on building a database but I can't figure out for the life of me if I am supposed to have the FK on the side of the many or the one.

    I was also confused as to whether I had to use the PK of the database that the FK was comming from or could I just use any field as the FK?

  2. #2
    Join Date
    Oct 2010
    Posts
    2
    Ok I'll try and explain.

    First of all you are really referring to tables in a database, not multiple databases. A database by definition contains one or more tables (and other stuff as well) which are related in some way. Hence the term Relational Database.

    Think of one to many as the relationship between a parent and their children. A parent (the ONE) can have many children (the MANY).

    From a database persepective we can think of it this way:

    Table A contains all the names of the parents. Table B contains all the names of the children. How do we associate one with the other?

    We need a way to uniquely identify parents in Table A. So in table A we create a field (you can have as many as you need, this is just for illustrative purposes) that will be the PK. Let's use social sec number and call it PARENT_SSN. Therefore there can never be more than one record with the same SSN. This is known as the Primary Key (by definition a PK field in a record will never allow duplicates). However, in MS ACCESS some one chose to allow this and violate all the rules...but I digress.

    In Table B we also need a unique field. We will also use SSN. However, we also need a way to associate multiple records (children) to one parent.

    In Table B we create two fields. Field 1 we call CHILD_SSN and Field 2 we call PARENT_SSN.

    We set up both these fields to be the PK of Table B. This allows for multiple rerocds with the same PARENT_SSN but different CHILDREN_SSN. The PARENT_SSN field in Table 2 is known as the Foreign Key, because it referrs to the PK of another table.

    For this particular example there is way to do the parent-child relation in one table using a recursive set up. However, I don't want to confuse you at this point.

    So. how can one visually see this relationship? Very easily. All database apps like MS ACCESS, Paradox, VB let you create forms with data grids on them. Additionally they allow you to create a Relationship Diagram that illustrates how the one>many relationship is defined. So in this case we would place both tables in the RD and define a link between the PARENT_SSN field of Table A and the PARENT_SSN of Table B.

    Once you do this you can now place two data grids on a form, one tied to TA and one tied to TB. Since the relationship has already been defined in the RD what you see is that as you navigate through the records in TA, TB only shows the records that are related.

    Sorry this is so long.....hope it helps

  3. #3
    Join Date
    Oct 2010
    Posts
    2
    I studied like crazy before the midterm and was able to get it figured out thank goodness to. That was one of the question. Which side does the Foreign Key go on? From my research I gathered that it should go on the many side of the relationship.
    I messed up my first time and just put foreign keys and primary keys in just about every table till I ended up with a huge spider web ERD that had many tables pointing to many other tables.

    Thanks again for helping me.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •