FK / PK 和 FK 空问题

发布于 2024-10-08 09:35:44 字数 593 浏览 0 评论 0原文

我正在为我的用户内容网站设计一些表格,但我对这种情况的 FK 概念迷失了。这两种情况有什么不同:

情况 A:
好友表
用户 ID (FK)
friend_id(FK)

案例 B:
好友表
ID(主键)
用户 ID (FK)
friend_id(FK)

我想要理解的是一张表有 2 个 FK,而 1 个表有 2 个 FKS + 1 个 PK。我见过的大多数朋友的模式都有案例 A,只有 2 个 FK,没有 PK。但我看到的大多数其他表都有 PK 的情况 B。那么这两个表有什么区别/优点/缺点以及使用哪一个呢?


我遇到的第二个问题是,如果我在表 A 中有一个 PK 并将其用作表 B 中的 FK,那么它在表 B 中是否必须为 NOT NULL,或者即使在表 A 中作为 PK 必须为 NULL,也可以为 NULL不为空?一个例子是绘制城市和州的地图。状态表中的状态必须为 NOT NULL。在城市表中,我将所有值存储为 NOT NULL DEFAULT 0,因为城市没有国家(世界城市),但我不想在数据库中存储 NULL,所以我为所有空值写入 0 只是为了保持单元格被占用。

I am designing some tables for my user content website and I am lost on the FK concept for this situation. What is the different in these two cases:

Case A:
Friend table
user_id (FK)
friend_id(FK)

Case B:
Friend table
id (PK)
user_id (FK)
friend_id(FK)

What I am trying to understand is one table has 2 FKs and 1 has 2 FKS + 1 PK. Most schema I have seen for friends have Case A that is only 2 FKs and no PK. But most other tables I see have case B of a PK. So what is the difference/advantage/disadvantage with both these tables an to use which one?


Second question I had is if I have a PK in table A and use it as a FK in table B, does it have to be NOT NULL in table B or can it be NULL even though in table A being a PK is has to be NOT NULL? An example of this is mapping City and State. State in state table is required as NOT NULL. In city table I am storing all values as NOT NULL DEFAULT 0 as not cities have states (world cities) but i dont want to store NULL in db so i am writing 0s for all null values just to keep the cells occupied.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枉心 2024-10-15 09:35:44

在情况 A 中,虽然 user_id 和friend_id 是外键,但它们很可能都是复合主键。在情况 B 中,有一个单独的 PK (id) 来标识每个独特的友谊。他们实际上都做同样的事情,但根据我的观点,第二种情况中的id是多余的,因为 PK(user_id,friend_id) 可以一起行动成为一个复合主键,它将唯一地标识每个友谊。我建议你读一本关于标准化的好书:)

In Case A, while user_id and friend_id are FK's they both are most probably a composite primary key. In Case B, there is a separate PK (id) to identify each unique friendship. They both actually do the same thing but according to my opinion, the id in 2nd case is redundant as the PK(user_id,friend_id) can act together to become a composite primary key which will uniquely identify each friendship. I suggest you read a good book on normalization :)

我不会写诗 2024-10-15 09:35:44

根据给出的信息,A和B的区别在于B多了一个列和键约束。哪种设计有意义实际上取决于需求以及数据对您意味着什么。

A 中的主键可能实际上是复合键:(user_id,friend_id)。在这种情况下,两种选择之间的区别在于 A 不允许用户和朋友的重复组合,而 B 则允许这些重复。这是一个显着的差异。

最好避免可为空的外键。如果您有一个并不总是适用的属性,那么您可以随时重新设计表,这样就不需要有可为空的列。将州放入另一个表中,如果该城市没有州,则无需填充该表。

According to the information given, the difference between A and B is that B has an extra column and key constraint. Which design makes sense really depends on the requirements and what the data means to you.

It may be that in A the primary key is actually the compound key: (user_id, friend_id). In that case the difference between the two alternatives is that A does not allow duplicate combinations of user and friend whereas B does allow those duplicates. That's a significant difference.

Nullable foreign keys are best avoided. If you have an attribute that doesn't always apply then you could always redesign your tables such that you don't need to have a nullable column. Put the state in another table which you don't need to populate if the city doesn't have a state.

浮云落日 2024-10-15 09:35:44

推荐情况A。

当您需要将长复合键迁移到另一个表时,通常会使用情况 B。例如,假设我们需要在表 T1 和 T2 之间创建多对多关系 (T3)。

Case A (natural keys):
table T1(pk1, pk2, pk3, pk4, someValue)
table T2(pka, pkb, pkc, pkd, otherValue)
table T3(pk1, pk2, pk3, pk4, pka, pkb, pkc, pkd, create_date)

需要三个唯一索引(每个表一个)。直接查询T3很方便。

Case B (surrogate keys):
table T1(t1_id, pk1, pk2, pk3, pk4, someValue)
table T2(t2_id, pka, pkb, pkc, pkd, otherValue)
table T3(t1_id, t2_id, create_date)

需要五个唯一索引。与情况 A 中的三个相同,每个代理键加一个。
T3 现在变得小得多,但代价是不再能够在不连接回 T1 或 T2 的情况下查询 M:M 关系。对于任何典型的应用程序来说,这可能是也可能不是一个大问题。

我认为这已经概括了一切。

Case A is recommended.

Case B is often used when you need to migrate a long composite key into another table. For example, let's say that we need to create a Many-to-Many relationship (T3) between tables T1 and T2.

Case A (natural keys):
table T1(pk1, pk2, pk3, pk4, someValue)
table T2(pka, pkb, pkc, pkd, otherValue)
table T3(pk1, pk2, pk3, pk4, pka, pkb, pkc, pkd, create_date)

Three unique indexes are needed (one in each table). It is easy to query T3 directly.

Case B (surrogate keys):
table T1(t1_id, pk1, pk2, pk3, pk4, someValue)
table T2(t2_id, pka, pkb, pkc, pkd, otherValue)
table T3(t1_id, t2_id, create_date)

Five unique indexes are neeed. The same three as in case A, plus one for each surrogate key.
T3 now becomes much smaller, but at the expense of no longer being able to query the M:M relationship without joining back to either T1 or T2. This may or may not be a big deal for any typical application.

I think that pretty much sums it up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文