• veritas_86 (5/26/2009)


    so I am creating a database where I have a parent table with multiple columns that I would like to have reference a single column in a child table. I would like to create relationships between the columns in the parent table and the column in the child table. For an example, I'm passing this to SQL

    parent table:

    CREATE TABLE Parent(val1 int NOT NULL, val2 int NOT NULL, val3 int NOT NULL, val4 int NOT NULL, PRIMARY KEY (val1), UNIQUE (val2), UNIQUE(val3), UNIQUE(val4))

    child table:

    CREATE TABLE Child(val1 int NOT NULL, val2 int, val3 int, val4 int, PRIMARY KEY(val1))

    relationships

    ALTER TABLE Child ADD FOREIGN KEY (val1) REFERENCES Parent(val2)

    ALTER TABLE Child ADD FOREIGN KEY (val1) REFERENCES Parent(val3)

    ALTER TABLE Child ADD FOREIGN KEY (val1) REFERENCES Parent(val4)

    These relationships are added fine, but when I try to update data to the child table, I get an error because columns 'val1' will contain data from many columns in the parent table, but each relationship requires that each value only appear in the referenced column in the Parent table. So my understanding is that this format will not work. However, I want the values in the Child table to column to be referencing the values in all the Parent table columns. Is there a way to get this to work? Thanks for your help!

    Altough it is possible to use a unique column to be referenced in a FK definition, you can off course only reference to a single column having the common values !

    Meaning, you can indeed - as youd did - reference multiple times, to diffenrent colmuns, but unless the ne value is available in all the referenced columns, the update cannot be done because it will violate the FK relation.

    That is why you can only define one primary key on a table.

    The goal is that only a primary key will be used for FK relationships.

    So, if anyone is pointing to a unique constraint, you should actually convert that to use the PK of that unique key.

    Also keep in mind, you should know where the value came from !

    There is no "try this or maybe that" option when it comes to data integrity !

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me