Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Multiple foreign keys

Author  Topic 

lb6688
Starting Member

18 Posts

Posted - 2010-08-22 : 15:16:33
Parent table (P_1) has two foreign keys on same column (recordno) link to two child tables (c_1, c_2), how would you know, just from the parent table, which recordno/row links to C_1 and which recordno/row link to C_2

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2010-08-22 : 17:32:03
post DDL
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-23 : 04:50:09
you need to left join from parent to both child tables and based on from which table you get matching values you can determine which record links to which table.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

lb6688
Starting Member

18 Posts

Posted - 2010-08-23 : 12:17:00
Got it, thanks visakh16. I thought there might be a system variable I can use for that.

Thanks again...
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2010-08-23 : 12:55:17
Is it possible for a each child table to have the same recordno?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-23 : 12:59:31
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

lb6688
Starting Member

18 Posts

Posted - 2010-08-23 : 22:51:48
report back to visakh16, work like a charm. Thanks again.

Lamprey, I do not think it's possible (visakh16, correct me if I am wrong), that's what "enforce foreign key constraint" for, isn't it?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-24 : 07:32:07
quote:
Originally posted by lb6688

report back to visakh16, work like a charm. Thanks again.

Lamprey, I do not think it's possible (visakh16, correct me if I am wrong), that's what "enforce foreign key constraint" for, isn't it?


Welcome
For second part, its possible. "enforce foreign key constraint" just make sure value put in either of child tables is a valid value in parent table. It doesnt check whether this value was already populated in any of child tables. So there can be chance that you may have same records in both the child tables unless you do check explicitly.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -