I have a many-to-many relationship between two models: customers and addresses. A customer has many addresses, but one of them would be a default address. I figured creating the junction table with a customer_id
, address_id
, and default
columns would be the way to go. The default
column would be a boolean. But is that right? There would be mostly 0 values, with the occasional 1.
我在两个模型之间有多对多的关系:客户和地址。客户有许多地址,但其中一个地址是默认地址。我想用customer_id,address_id创建联结表,默认列是可行的方法。默认列是布尔值。但那是对的吗?大多数会有0个值,偶尔会有1个值。
Another person told me to just keep the junction table without the default
column, and instead save the default address in the customers
table as address_id
. The argument was for storage space since the extra column in the junction table is not needed, and that we don't need an extra join to get the default address ID.
另一个人告诉我只保留没有默认列的联结表,而是将customers表中的默认地址保存为address_id。该参数是针对存储空间的,因为不需要连接表中的额外列,并且我们不需要额外的连接来获取默认地址ID。
However, from a data structure standpoint, I think having the default flag in the junction table makes more sense. Also, storage is not going to be an issue.
但是,从数据结构的角度来看,我认为在联结表中使用默认标志更有意义。此外,存储不会成为问题。
I'd love to hear some other input on this and any best practices. Thanks in advance.
我很想听听关于这个和任何最佳实践的其他意见。提前致谢。
1 个解决方案
#1
0
Just have a many:many relationship ("junction table") "customer CUSTOMER has address ADDRESS" and a 0-or-1-to-0-or-1 "customer CUSTOMER's default address is ADDRESS". There's a foreign key on (CUSTOMER, ADDRESS) from the latter to the former. If a default address is optional and you want to have it as a property of a customer then you could make it NULLable. (But this introduces a circularity involving foreign keys which most DBMSs needlessly choke on.)
只有很多:很多关系(“联结表”)“客户CUSTOMER有地址ADDRESS”和0或1到0或1“客户CUSTOMER的默认地址是ADDRESS”。从后者到前者的(CUSTOMER,ADDRESS)有一个外键。如果默认地址是可选的,并且您希望将其作为客户的属性,则可以将其设置为NULLable。 (但这引入了涉及外键的循环,大多数DBMS都不必要地扼杀了它。)
There is no point in trying to "optimize" without many more specifics in place, plus a smell, plus patterns of use, plus present cost-benefit of options, plus (if done) verification. You seem still far from the experience necessary to persue that process. Just make the most straightforward design.
没有更多具体细节,加上气味,使用模式,加上目前的选择成本效益,加上(如果完成)验证,尝试“优化”是没有意义的。你似乎仍然远没有完成这个过程所需的经验。只需做出最直接的设计。
#1
0
Just have a many:many relationship ("junction table") "customer CUSTOMER has address ADDRESS" and a 0-or-1-to-0-or-1 "customer CUSTOMER's default address is ADDRESS". There's a foreign key on (CUSTOMER, ADDRESS) from the latter to the former. If a default address is optional and you want to have it as a property of a customer then you could make it NULLable. (But this introduces a circularity involving foreign keys which most DBMSs needlessly choke on.)
只有很多:很多关系(“联结表”)“客户CUSTOMER有地址ADDRESS”和0或1到0或1“客户CUSTOMER的默认地址是ADDRESS”。从后者到前者的(CUSTOMER,ADDRESS)有一个外键。如果默认地址是可选的,并且您希望将其作为客户的属性,则可以将其设置为NULLable。 (但这引入了涉及外键的循环,大多数DBMS都不必要地扼杀了它。)
There is no point in trying to "optimize" without many more specifics in place, plus a smell, plus patterns of use, plus present cost-benefit of options, plus (if done) verification. You seem still far from the experience necessary to persue that process. Just make the most straightforward design.
没有更多具体细节,加上气味,使用模式,加上目前的选择成本效益,加上(如果完成)验证,尝试“优化”是没有意义的。你似乎仍然远没有完成这个过程所需的经验。只需做出最直接的设计。