SQL 成员表显示成员之间的家庭关系
我几天来一直在尝试设计这个数据库。我在 access 中设计它,然后在 MYSQL Server 上实现为 PHP Web 应用程序。
我有一张表:
Members
- MemberID (autoNumber, PK)
- MemberName
- MemberDetails
成员可以与其他成员有很多关系,这可能是孩子、父母、朋友、配偶等。查看一个成员,我会希望能够与现有成员创建新的关系,然后让相关成员也可以看到该关系,而无需进一步输入。成员还应该能够列出他们所有的关系。
你能告诉我应该怎么做吗?我尝试了一些选项,但似乎没有一个能按预期工作。我对 SQL 很满意,只是在一元关系设计方面遇到了麻烦。
-编辑- 另外,我忘了补充一下,由于服务器限制,这将无法使用 INNODB。最有可能的是 MYISAM,尽管我仍然想要引用完整性:(
I've been trying for days to design this database. I'm designing it in access then implementing on MYSQL Server as a PHP Web Application.
I have a table:
Members
- MemberID (autoNumber, PK)
- MemberName
- MemberDetails
Members can have many relationships with other members, this may be Child, Parent, Friend, Spouse etc. Looking at one Member, I'd like to be able to create new relationships with existing members and then have that relationship also be visible from the related member without further input. Members should also be able to list all their relationships.
Can you please advise how I should do this? I've tried a few options but none seem to work as intended. I'm comfortable with SQL, I'm just having trouble with the Unary relationship design.
-edit-
Also, I forgot to add, this is not going to be able to use INNODB due to server restrictions. Most likely will be MYISAM, though i still want referential integrity :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
让表
members
包含成员数据,表relations
包含成员关系数据。relations.member_id
将是对成员的引用,relations.lated_member_id
- 相关成员的引用。relations.type
是可枚举的关系类型。UPD: MyISAM 版本(删除了外键,所有可能的外键功能应由服务器端脚本处理):
Let the table
members
contain members data and the tablerelations
contain member relations data.relations.member_id
will be a reference to the member,relations.related_member_id
- to the related member.relations.type
is enumerable relation type.UPD: MyISAM version (removed foreign keys, all possible foreign keys functionality should be handled by server side scripts):
从@Minras设计(+1)开始,我
会添加第三个表,而不是检查约束:
RelationType是整数,“标签”是字符数据。该关系是“定向”的,因为您必须密切注意哪个成员是“来自”,哪个是“到”(但对于“非定向”关系并不那么重要,例如“上过高中”)一起”)。这种设计将允许您:
A 是 B 的父亲
,B 是 A 的儿子
。显然,您要么通过外键或任何可用的东西在所有事物上保持关系完整性,要么您将等待发生火车失事。
这并没有解决如何唯一、清晰地识别重名成员的问题。为此,您要么需要考虑现实世界中人们用来处理此类情况的识别属性(学生 ID?社会安全号码?),要么引入特定于您的应用程序的工件(例如,登录名 + 密码) )。
Starting with @Minras design (+1), I’d have
But instead of a check constraint, I’d add a third table:
with RelationType being an integer and the “Labels” being character data. The relation is "directional", in that you'll have to pay close attention to which member is "From" and which is "to" (but not so important for "non-directional" relations, such as "went to high school together"). This design will allow you to:
A is father of B
andB is son of A
.Obviously you’ll either have relational integrity on everything via foreign keys or whatever you have available, or you’ll have a train wreck waiting to happen.
This does not address the issue of how to uniquely and clearly identify members with duplicate names. To do that, you either need to factor in the identifying attribute” used by people in the real world to deal with such situations (student ID? social security number?), or introduce an artifact specific to your application (say, login + password).
尝试添加数据透视表:
Try adding a pivot table:
创建一个带有列的表:
然后您可以像这样使用它:
如果爱丽丝是鲍勃的女儿,您将拥有这些关系:
然后您可以在这些关系中添加一些额外的数据,例如关系何时开始(即某人何时订婚)。
您可能还想在具有成员 ID 的两列上创建索引。
编辑:
为了避免在该表中重复数据,您可以创建一个视图,然后将每个关系仅存储为一行。然而,这个解决方案不允许您命名诸如“父亲”和“儿子”之类的关系。
Create a table with collumns:
Then you can use it like that:
If Alice is Bobs daughter you'll have those relations:
You can then throw some additional data into those relationships like when did the relation begin (ie. when someone got engaged).
You may also want to create indexes on both columns with member ids.
EDIT:
To avoid duplicating data in this table you can create a view and then store every relation as only one row. However this solution won't allow you to name relations like 'father' and 'son'.