-1

current situation:

I have a table A which has a column. This column should have a foreign key relationship to either C or D

How can I describe this in SQL Definition Language

To be more clear:

C and D are e.g. tables with information about Taxes. But this taxes are not in one table. They are distributed in this two tables.

What I now want ist that the Foreign Key Reference in table A should have a Foreign Key reference at the same time on the same column e.g. A->tax_rate_id to either Table C or D.

Is this possible and how to write the DDL description for it

lennykey
  • 1,195
  • 2
  • 12
  • 25
  • Hi. This is a faq. Always google many clear, concise & specific versions/phrasings of your question/problem/goal & read many answers. Add relevant keywords you discover to your searches. If you don't find an answer then post, using use one variant search for your title & keywords for your tags. Eg google your current title. PS Google re database/SQL subtyping/poymorphism/hierarchies for how to avoid this antipattern. – philipxy Apr 15 '18 at 02:50
  • Possible duplicate of [Foreign Key to multiple tables](https://stackoverflow.com/questions/7844460/foreign-key-to-multiple-tables) – philipxy Apr 15 '18 at 02:51

1 Answers1

1

A foreign key cannot have reference to 2 tables at once. You should read the definition of foreign keys.

Amin
  • 38
  • 1
  • 8
  • Hi, @amin I tried to update the description of the question – lennykey Apr 12 '18 at 07:18
  • Edited the answer. – Amin Apr 12 '18 at 10:16
  • Yes, "FK" has a definition & it means a certain thing & one has one target. But what does "this isn't even possible in real world" mean? A column set sure can be constrained to have a value in one target or (exclusively or inclusively) another. It's just not a FK constraint. – philipxy Apr 15 '18 at 02:48
  • Yes you're right. We have, but I think it's rare for them to be primary in both tables. Anyway answer edited. – Amin Apr 15 '18 at 06:53