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 reference in a single field Expand / Collapse
Author
Message
Posted Monday, April 12, 2010 4:33 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, June 5, 2014 2:14 PM
Points: 2, Visits: 26
Hi Dudes,

I need to have a foreign key field that should refer from two tables. for an example,

table_a
--------
1 a_id primary key,
2 name

table_b
--------
1 b_id primary key,
2 name

table_c
-------
1 c_id
2 name
3 key_id (i.e, it should be foreign key from either table_a or table_b. it may have value of either table_a's id or table_b's id

Can any one of u help me to get this script to create table.

Thanks
Saga
Post #901450
Posted Monday, April 12, 2010 5:33 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 9:56 AM
Points: 7,120, Visits: 13,491
How will you determine which lookup table the foreign key value is from, for each row of table c?

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #901489
Posted Monday, April 12, 2010 5:58 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 3:30 AM
Points: 1,059, Visits: 692
hi,

I think its not possible to do......

Try to do the following one...

For this U should add another column in TableC.
Key_IDa is foreign key and references to Table A PK.
Key_IDb is foreign key and references to Table B PK.


Why dont you check with the Wizard options availed from SSMS.
For relationships btwn tables ,Creating Foreign Keys........



Thanks!
Sasidhar Pulivarthi
Post #901505
Posted Tuesday, April 13, 2010 11:15 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, January 28, 2014 8:15 AM
Points: 3,068, Visits: 4,639
Sasidhar is correct.

FKs are there to enforce referential integrity rules. No referential integrity rules takes into consideration one child row with two alternative parents.

If this is a business spec it has to be manually enforced.


_____________________________________
Pablo (Paul) Berzukov

Author of Understanding Database Administration available at Amazon and other bookstores.

Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.
Post #902638
Posted Tuesday, April 13, 2010 11:46 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Thursday, June 5, 2014 10:54 AM
Points: 9,902, Visits: 9,480
You cannot enforce this with static RI (referential integrity) without the use of an artificial "helper" table, which is a bit of a mess to setup and maintian and not recommended. The recommended alternative is to use dynamic RI, (which almost always means triggers) to enforce this constraint.

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
Post #902651
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse