1

What is the best way of dealing with foreign keys in a table that can be from two different tables? To be more clear:
T1(id,c1,c2) - c1 and c2 are from either T2 or T3
T2(id,name)
T3(id,name)
Consider T2 and T3 have to be different tables. And if c1 is from T1 (for example), c2 can be from either T2 or T3.

Should I:

1. Create a new table that holds the associations and have c1 and c2 point to an id from that table? i.e. T4(id,id_from_T1,id_from_T2_or_T3,what_table)

2. Create 2 columns, c1_parent, c2_parent and make a convention like if it is 1 then it's from T2 if it's 2 then it's from T3.

Or is there an entirely new better way?

Thank you.

asked Aug 10, 2011 at 7:29
2
  • Do T2 and T3 have the same structure? Commented Aug 10, 2011 at 7:30
  • No, I'd have to use IF and CASE. But for the moment they both share than name column which is all I need. Not sure in the future. Commented Aug 10, 2011 at 7:33

2 Answers 2

2

Conditional foreign keys are non-standard SQL. You should find a way to amalgamate your NAMES tables. If necessary, NAMES could have a composite key.

For example, instead of DEFENDERS and ATTACKERS tables, you'd have amalgamated PLAYERS table with a column that indicated whether the player was offense or defense:

 PLAYERS
 OffenseOrDefense
 PlayerName
 primary key(OffenseOrDefense,PlayerName)
 DREAM TEAM
 position
 offenseOrDefense
 playerName
 foreign key(offenseOrDefense,PlayerName) references PLAYERS(offenseOrDefense,PlayerName)
answered Aug 10, 2011 at 11:13
Sign up to request clarification or add additional context in comments.

Comments

1

There isn't a good mechanism for guaranteeing that id is not duplicated across all three tables. So you're better off with T1 as:

T1(id,c1_tbl,c1_id,c2_tbl,c2_id)

Alternatively, refactor your table design so that a given foreign key can only come from one specific table. (I can't give specifics because you didn't describe the goals of this schema.)

Either of these will make life easier when it comes time to JOIN.

answered Aug 10, 2011 at 8:00

2 Comments

Thank you Mike. But can you condition JOINs based on a column?
Sure. I don't write a lot of SQL myself, but I'd imagine it would be something like SELECT ... FROM T1, T2 WHERE T1.c1_tbl = 1 INNER JOIN T1.c1_id = T2.id or so. The Django contenttypes framework uses something like this. It creates a table that represents each table that can be used in this way, assigning each table an ID number. Then you can reference any record in any table with a table-ID and ID-within-table combination (a "generic foreign key"), allowing the IDs in the tables to be standard auto-increment fields.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.