Need help in establishing table relationships

  • Hello everyone. I have 3 table in my database among which I'm trying to establish relationships. I've been able to establish a relationship between 2 of them. The Customer table which has all the clients which appear only once. The primary key for this table is the Client Number field. I also have a Volume table that contains all the volumes keyed for each customer. As you would expect the same customer will appear numerous times in this table. I established a one-to-many relationship between these two tables connecting the prmary key of the Customer Table and the BoxNumber field of the volume table.

    Looking at the Transaction code table, you'll notice that a single customer may have serveral codes that apply to them. Everything works well until I try to establish a relationship between the Customer table and the Transaction code table. At that point things begin to double. Why is this happening? And can I fix it so that I can use all 3 tables? 

    Customer Table

    Client NameClient Number (primary key)
    96001 / 70840 - Wachovia Mortgage Corp.1
    96011 Clayton (Vanderbilt) Mortgage11
    96012 / 70829 - Home Equity12
    96013 - World Omni13
    96014 - World Omni14
    96019 - ALLTEL Communications - Cellware19
    96002 - Vehicle Leasing2
    96020 - Vehicle Leasing20
    96022 - Worldcom22
    70884 - Capital One Bank23
    96024 - World Omni Finance24
    70881 - Virginia Natural Gas248
    70885 - Capital One FSB25
    96026 / 70835 - BTI Corp.26
    96028 - Sprint SubDoc28

    ---------------------------------------------------------------------

    Volume Table

    ShiftSiteIDBox Number

    Foreign Key

    Image Items
    1st"CH"1August20048/10/2004209
    1st"CH"11August20048/27/200423
    1st"CH"12January20061/10/20062
    1st"CH"19April20054/29/200519604
    1st"CH"23August20058/26/200553880
    1st"CH"248December200412/14/20041512
    1st"CH"25February20062/17/20069384
    1st"CH"26May20055/4/2005409
    1st"CH"28January20061/9/20060
    1st"CH"3February20062/24/200634
    1st"CH"31July20047/21/2004227
    1st"CH"375January20051/31/2005706
    1st"CH"40December200512/1/20057595
    1st"CH"487March20053/8/2005191

    ------------------------------------------------------------------------------

    Transaction Code Table

    BoxNumber

    Foreign Key

    Transacation CodeDefinitionsVolume
    261901Lockbox Rental0
    261900Monthly Maintenace0
    261908Multiple Mailout0
    261910Special Audits Mail Study0
    261911Special Logs0
    261912Interoffice Mail Per Box0
    261914Courier Charges0
    261915Data Transmission0
    261917Stop File0
  • When you say "everything begins to double," I'm guessing you mean in a SELECT query involving all three tables. Correct? And, because you mentioned doubling, I'm going to go out on a limb and guess that there are two records in the volume table for Customer 26.

    Now, when I look at your sample data, I see a Volume column in the Transaction Code Table. Is that intended to be related to a row in the Volume Table? It's hard to tell, as you didn't list a primary key for the Volume Table (you do have a primary key on each table, don't you?).

    However, if you write your query as you described, it would likely look like:

    SELECT Customer.*, Volume.*, Transaction.*

    FROM Customer

    JOIN Volume ON Customer.ClientNumber = Volume.BoxNumber

    JOIN Transaction ON Customer.ClientNumber = Transaction.BoxNumber

    This will essentially give you a cross-product of the Volume and Transaction tables, limited only where they share a Box Number. Why? Simply because there's no relationship between the Volume and Transaction tables. If you have two Volumes, and no way to tell which Transaction goes with which Volume, it will just give you all the Transactions on both Volumes. It's the only way SQL has to deal with that scenario.

    So, based on the sample data, I would suggest creating a relationship between Transaction and Volume, and having that inherit the relationship between Volume and Customer.

Viewing 2 posts - 1 through 1 (of 1 total)

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