开发者

Database table with foreign key to multiple tables

开发者 https://www.devze.com 2023-03-26 21:58 出处:网络
I have a table with a column that is an I开发者_如何学JAVAD to either table T1 or table T2. Is it a good idea to add an extra column that indicates if that ID is from T1 or T2, or should I do it diffe

I have a table with a column that is an I开发者_如何学JAVAD to either table T1 or table T2. Is it a good idea to add an extra column that indicates if that ID is from T1 or T2, or should I do it differently?


You'll probably be happier with two columns: one a foreign key to T1, the other a foreign key to T2.


The answer depends on the context of the application. There are a number of cases where an architecture like the one you are using would be very dynamic. However, to use this efficiently with user interfaces for CRUD operations, you'd want views of the data limited by the indicator column you mentioned. Really, the merit there is that when you want to add table 3, you can just add another indicator value.

If the application you are working on is fairly simple or will use a number of known data entry screens and will not automatically support the data in a new table, then I agree with the other answer that has been posted, suggesting separate foreign key columns.

0

精彩评论

暂无评论...
验证码 换一张
取 消