设置默认标志 ​​- 多对多关系

时间:2021-05-29 04:04:44

In this example, there are two companies and addresses. This is a many-to-many relationship. A company must have at least one address, but it can have more. Likewise, an address can be tied to more than one company (think that they're in the same building).

在此示例中,有两个公司和地址。这是一种多对多的关系。公司必须至少有一个地址,但它可以有更多。同样,地址可以绑定到多个公司(认为它们位于同一建筑物中)。

So there is a join table. I'd like to note what the default address will be for a given company, so I figure the table would have three columns: company_id, address_id, is_default.

所以有一个连接表。我想说明给定公司的默认地址是什么,所以我认为该表有三列:company_id,address_id,is_default。

My question is more about the is_default column. Knowing that there will be many rows with 0 in that column, and then 1 for the default, is this an acceptable way to handle noting which address is the default?

我的问题更多的是关于is_default列。知道该列中有许多行为0,然后默认为1,这是一种可接受的方法来处理注意哪个地址是默认的?

I figure I can put a unique constraint using the company_id and is_default column, but was wondering if anyone would see an issue with the low cardinality of the is_default column. The question is more in a general sense, as I know it depends on the situation, and I've only provided some details. But also to note, there will not be a massive number of rows in the table either.

我想我可以使用company_id和is_default列放置一个唯一的约束,但是想知道是否有人会看到is_default列的低基数问题。问题在于一般意义上的问题,因为我知道这取决于具体情况,我只提供了一些细节。但另外需要注意的是,表中也不会有大量的行。

Thanks in advance.

提前致谢。

1 个解决方案

#1


1  

The low cardinality won't be a problem. Your attribute is boolean, so it needs no more than one bit per record, so considering hardware prices, even billions of records won't make any difference.

低基数不会成为问题。您的属性是布尔值,因此每条记录需要不超过一位,因此考虑到硬件价格,即使数十亿条记录也没有任何区别。

There is an alternative which might be more practical in terms of updating and querying: Have an additional default_address_id column for your companies which takes the id of the default address record. If you make that not null, you can make sure that every company has exactly one default address without overstressing the not-too-strong constraint system of MySQL. But no matter how you decide, it won't break your neck.

在更新和查询方面,有一种替代方案可能更实用:为公司添加一个额外的default_address_id列,其中包含默认地址记录的id。如果你使它不为null,你可以确保每个公司只有一个默认地址,而不会过度强调MySQL的不太强大的约束系统。但无论你怎么决定,它都不会打破你的脖子。

#1


1  

The low cardinality won't be a problem. Your attribute is boolean, so it needs no more than one bit per record, so considering hardware prices, even billions of records won't make any difference.

低基数不会成为问题。您的属性是布尔值,因此每条记录需要不超过一位,因此考虑到硬件价格,即使数十亿条记录也没有任何区别。

There is an alternative which might be more practical in terms of updating and querying: Have an additional default_address_id column for your companies which takes the id of the default address record. If you make that not null, you can make sure that every company has exactly one default address without overstressing the not-too-strong constraint system of MySQL. But no matter how you decide, it won't break your neck.

在更新和查询方面,有一种替代方案可能更实用:为公司添加一个额外的default_address_id列,其中包含默认地址记录的id。如果你使它不为null,你可以确保每个公司只有一个默认地址,而不会过度强调MySQL的不太强大的约束系统。但无论你怎么决定,它都不会打破你的脖子。