SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Multiple columns and foreign key relationships


Multiple columns and foreign key relationships

Author
Message
veritas_86
veritas_86
SSC Veteran
SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)SSC Veteran (218 reputation)

Group: General Forum Members
Points: 218 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!
ALZDBA
ALZDBA
SSC-Dedicated
SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)SSC-Dedicated (30K reputation)

Group: General Forum Members
Points: 30163 Visits: 8986
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


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


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


press F1 for solution, press shift+F1 for urgent solution :-D


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me Alien but most of the time this is me Hehe
Jack Corbett
  Jack Corbett
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45069 Visits: 14925
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
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search