在两个DB表中有循环引用是一个好习惯 [英] Is it a good practise to have circular reference in two DB tables

查看:164
本文介绍了在两个DB表中有循环引用是一个好习惯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的DB中,我们有两个具有主键A_id和B_id的表A,B。



在表中使用B_id作为外键是一个好习惯A和A_id作为外键在表B中。这将允许我们在表中具有多对多关系。



另一种方法是使用第三个网桥您认为哪一个是好的做法?



< >解决方案

考虑以下情况

  TableA TableB 
A 1
B 2

如果你想交联这个,你最少需要做

  TableA 
A,1
A,2
,1
B,2

TableB
1
2

对于每个多对多关系,至少有一个表的每一行都重复



将不会找到许多DBA愿意模仿他们的表。

第三个桥接表真的是这里唯一好的选择。


In our DB we have two tables A, B with primary keys A_id and B_id.

Is it a considered a good practice to have B_id as foreign key in table A and A_id as foreign key in table B. This would allow us to have many-to-many relationship in the table.

An alternative would be to have a third bridge table consisting of just two columns A_id and B_id.

Which one do you think is a good practice?

解决方案

Consider following scenario

TableA  TableB
A       1
B       2

If you want to crosslink this, the least you need to do without creating a third table is

TableA
A, 1  
A, 2   
B, 1
B, 2

TableB
1
2   

Every row of at least one table is duplicated for every many-to-many relationship.

I think you won't find many DBA's willing to model their tables like that.
A third bridge table really is your only good option here.

这篇关于在两个DB表中有循环引用是一个好习惯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆