在两个DB表中使用循环引用是一种好习惯

时间:2021-05-28 12:33:17

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

在我们的数据库中,我们有两个表A,B,主键为A_id和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.

将B_id作为表A中的外键和A_id作为表B中的外键是一种良好的做法。这将允许我们在表中具有多对多关系。

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

另一种方法是使第三个桥表只包含两列A_id和B_id。

Which one do you think is a good practice?

您认为哪一个是好习惯?

5 个解决方案

#1


3  

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 duplicating every row in one of the two tables. I doubt you'll find many DBA's willing to model their tables like that.

如果要交叉这一点,那么在不创建第三个表的情况下,您需要做的最少的事情是复制两个表中的一个表中的每一行。我怀疑你会发现很多DBA愿意为这样的表格建模。

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

TableB
1
2   

A third bridge table really is your only good option here.

#2


7  

I think a bridge table would be ideal for implementing many to many relationship between two tables.And it is not a good practice to have a circular reference between tables.

我认为桥表对于实现两个表之间的多对多关系是理想的。并且在表之间进行循环引用不是一个好习惯。

#3


1  

It depends on the relationship between A and B, whether it is one-to-one, one-to-many, or many-to-many. In general, though, circular references are bad simply because they increase the amount of maintenance you have to do to keep the two tables in sync.

它取决于A和B之间的关系,无论是一对一,一对多还是多对多。但是,一般情况下,循环引用很糟糕,因为它们增加了维护两个表同步所需的维护量。

#4


1  

As wizzardz mentioned and espically with DBMSs, i'd try to avoid circular references.

正如wizzardz提到的并且特别是DBMS,我会尽量避免使用循环引用。

It has the potential of causing you a great deal of problems. Also if others will be working with that design, you'll have to nail down the documentation for it as they could end up going round in circles trying to work it out.

它有可能给你带来很多问题。此外,如果其他人将使用该设计,你将不得不确定它的文档,因为他们可能最终绕圈试图解决它。

#5


0  

What you call a bridge table is the normalization of a join dependency, and it's supported by good theory. You should not be recording the same fact in multiple locations.

你称之为桥接表的是连接依赖的规范化,并且它得到了良好理论的支持。您不应该在多个位置记录相同的事实。

#1


3  

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 duplicating every row in one of the two tables. I doubt you'll find many DBA's willing to model their tables like that.

如果要交叉这一点,那么在不创建第三个表的情况下,您需要做的最少的事情是复制两个表中的一个表中的每一行。我怀疑你会发现很多DBA愿意为这样的表格建模。

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

TableB
1
2   

A third bridge table really is your only good option here.

#2


7  

I think a bridge table would be ideal for implementing many to many relationship between two tables.And it is not a good practice to have a circular reference between tables.

我认为桥表对于实现两个表之间的多对多关系是理想的。并且在表之间进行循环引用不是一个好习惯。

#3


1  

It depends on the relationship between A and B, whether it is one-to-one, one-to-many, or many-to-many. In general, though, circular references are bad simply because they increase the amount of maintenance you have to do to keep the two tables in sync.

它取决于A和B之间的关系,无论是一对一,一对多还是多对多。但是,一般情况下,循环引用很糟糕,因为它们增加了维护两个表同步所需的维护量。

#4


1  

As wizzardz mentioned and espically with DBMSs, i'd try to avoid circular references.

正如wizzardz提到的并且特别是DBMS,我会尽量避免使用循环引用。

It has the potential of causing you a great deal of problems. Also if others will be working with that design, you'll have to nail down the documentation for it as they could end up going round in circles trying to work it out.

它有可能给你带来很多问题。此外,如果其他人将使用该设计,你将不得不确定它的文档,因为他们可能最终绕圈试图解决它。

#5


0  

What you call a bridge table is the normalization of a join dependency, and it's supported by good theory. You should not be recording the same fact in multiple locations.

你称之为桥接表的是连接依赖的规范化,并且它得到了良好理论的支持。您不应该在多个位置记录相同的事实。