简体   繁体   English

这实际上是一对多关系吗?

[英]Is this actually a one-to-many relationship?

I have three tables in my PostgreSQL database: 我的PostgreSQL数据库中有三个表:

User - Contains a username and password
MaleProfile - Contains information related to each male user
FemaleProfile - Contains information related to each female user

Initially, instead of having separate MaleProfile and FemaleProfile tables, I had a single Profile table. 最初,我没有单独的MaleProfile和FemaleProfile表,而是只有一个Profile表。 In that situation, I would have had a one-to-one relationship between the User table and the Profile table. 在这种情况下,我在User表和Profile表之间应该是一对一的关系。 But I've since decided that I really need separate profile tables for men versus women. 但是从那以后,我决定我真的需要为男性和女性提供单独的个人资料表。 In this new situation, each record in the User table must map to one and only one record in either the MaleProfile table or the FemaleProfile table (but not both). 在这种新情况下,User表中的每条记录都必须映射到MaleProfile表或FemaleProfile表中的一个且只有一个记录(但不能两者都映射)。 From the other direction, each record in the MaleProfile table maps to one and only one record in the User table. 从另一个方向来看,MaleProfile表中的每个记录都映射到User表中的一个且只有一个记录。 The same holds true for each FemaleProfile record. 每个FemaleProfile记录也是如此。

Strictly speaking, the relationship between the User table and each of the profile tables is one to zero-or-one. 严格来说,User表和每个概要文件表之间的关系是一对零或一对一。 But are these relationships essentially just one-to-many relationships in the sense that "many" in this case means just zero or one (but not more than one)? 但是,在这种情况下,“许多”意味着零或一(但不超过一)意味着这些关系在本质上仅仅是一对多关系吗? If so, would I express them as you would any one-to-many relationship by creating a foreign key column in the MaleProfile table and in the FemaleProfile table, each of which points to the PK column in the User table? 如果是这样,我是否会通过在MaleProfile表和FemaleProfile表中创建外键列来将它们表示为一对多关系,每个外键列都指向User表中的PK列? Would I need to add any additional constraints to the profile tables to maintain referential integrity? 我是否需要向配置文件表添加任何其他约束以维护引用完整性?

Thank you. 谢谢。

Just make sure the referencing column in your male/female tables has a uniqueness constraint on it as well as a foreign key constraint. 只需确保您的公/母表中的引用列具有唯一性约束和外键约束即可。 This is a 1-to-1/0 relationship, not 1-to-many. 这是一对一的关系,而不是一对多的关系。

CREATE TABLE MaleProfile
 (UserId INT NOT NULL PRIMARY KEY
 REFERENCES "User" (UserId));

I believe that you should put the employeeID number in the profile tables and enforcing a unique constraint in those tables. 我相信您应该将employeeID号放在配置文件表中,并在这些表中强制执行唯一约束。 Although I can't really think of how to keep someone from having both a male and female entry, I believe that this way is the way to go. 尽管我真的想不出如何阻止某人进入男性和女性的行列,但我相信这是一条路。

Check out https://stackoverflow.com/a/669015/1504882 to see a similar situation to yours, but instead with different types of employees. 请访问https://stackoverflow.com/a/669015/1504882,以查看与您类似的情况,但员工类型不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM