Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Multiple columns and foreign key relationships Expand / Collapse
Author
Message
Posted Tuesday, May 26, 2009 9:44 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, May 27, 2009 8:34 AM
Points: 22, Visits: 44
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!

Post #723355
Posted Tuesday, May 26, 2009 12:57 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, September 26, 2014 7:44 AM
Points: 6,731, Visits: 8,476
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


Don't drive faster than your guardian angel can fly ...
but keeping both feet on the ground won't get you anywhere

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


- 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
Post #723525
Posted Tuesday, May 26, 2009 3:19 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:23 PM
Points: 10,214, Visits: 13,161
It sounds like you want the value in the child table to match one of the columns in the parent table. Can you explain the business case for this? You may be offered another solution for this that works better.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #723645
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse