EER 图中主键和外键的使用
在我的数据库中,我有三个表(我有更多表,但情况相同,用户可以是公司或单身人士)。
Users
有一个主键id_user
;Company
有一个主键id_company
和一个外键users_id_user
;job_offers
有一个主键id_job_offers
和两个外键:company_id_company
和company_users_id_user
。
我的问题是:
- 主键在 job_offers 中有意义吗?我认为这是没有理由的。
job_offers
有两个外键,一个与company
相关,另一个与users
相关。这有问题吗?是否存在另一种方法来完成相同的任务?
In my db I have three tables (I have more but for case is equal, users can be companies or single people).
Users
has a primary keyid_user
;Company
has a primary keyid_company
and a foreign keyusers_id_user
;job_offers
has a primary keyid_job_offers
and two foreign keys:company_id_company
andcompany_users_id_user
.
My questions are:
- Does a primary key make sense in
job_offers
? I don't think that there is a reason for it. job_offers
has two foreign keys, one related tocompany
and other tousers
. Is there a problem with this? Does there exist another way to accomplish the same task?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
所有表都应该有一个主键。听起来您好像在问您的主键应该是代理键还是自然键。
您可能也会向其他桌子问同样的问题。例如,假设 users 表中的 email 列是必需的且唯一的,则它可以用作(自然)主键。
这个问题引起了激烈的争论,两种方法都可以工作(混合方法也可以)。如果您想总体了解这个主题,请在谷歌上搜索“自然与代理键”。
All tables should have a primary key. It sounds like you are asking whether your primary key should be a surrogate key or a natural key.
You might ask the same question of your other tables as well. For instance, assuming the email column in your users table is required and unique, it could be used as a (natural) primary key.
This question is pretty heavily debated, and both approaches can work (as can a mixed approach). If you want to read up on this subject in general, do a google search for "Natural vs. Surrogate Key".
是的 。我同意每桌都应该有自己的PK。
每个表都应该有一个主键吗?
但从您的 ER 图中,
Company.users_id_user
是 PK ,它不能为 null ,并且此 PK 在job_offers.company_users_id_user
中用作 FK。因此job_offers.company_users_id_user
也不能为 null 。因此,它无法处理公司用户刚刚发布职位而普通用户获得此工作机会之前或最终没有人获得此工作机会的情况。在这种情况下,
job_offers.company_users_id_user
应该设置为 null ,这违反了 job_offers.company_users_id_user 的非空约束。我将使用此设计完成相同的任务:
Yes . I agree that every table should have their own PK.
Should each and every table have a primary key?
But from your ER diagram ,
Company.users_id_user
is the PK , which cannot be null , and this PK is used in thejob_offers.company_users_id_user
as a FK. Sojob_offers.company_users_id_user
also cannot be null .As a result , it cannot handle the situation that a company user just post a job and before a normal user gets this job offer or no one gets this job offer eventually .In this case,
job_offers.company_users_id_user
should set to null , which violates thejob_offers.company_users_id_user
's not null constraint.I will accomplish the same task using this design:
是的 - 每个表都应该有一个主键。这就是所谓的“标准化”。
你的选择可能不是很好。我想说两个外键一起应该是主键,而不是 id 列。
不,这就是多对多关系的完成方式。
Yes there is - every table ought to have a primary key. It's called 'normalization.'
Your choice might not be very good. I'd say that the two foreign keys together should be the primary key, not the id column.
No, that's how many-to-many relationships are done.
id
field there. The two foreign keys should, together, make up the table's primary key.