I have a conceptual question that I’m hoping someone more versed in sql database design can help me with. I have a table in which each row has a corresponding row in one of several other tables. Ie. rows in table 1 have a corresponding row in either table 2, table 3 or table 4 (but NEVER in more than 1… the corresponding row can only be in one of the other tables).
What is the best way to set this structure up. If I put an othertable_id column and a tablename column in table 1, I can guarantee that there is only 1 corresponding row in the other tables, but it seems like a really inflexible messy solution. On the other hand if I put just a table1_id column in table2, table3, and table4, it seems like I will need to run 3 different queries each time I want to find the row that corresponds to a row in table1, AND it seems like I cannot guarantee there only being one entry in any of the three tables for my row in table1.
Does anyone have any suggestions?
I would use the second solution, and use a trigger to ensure no more than one related row exists.
The query would look like:
If there was a common column among the tables you wanted, e.g.,
value, you could get it like this: