SQL 成员表显示成员之间的家庭关系

发布于 2024-12-22 09:14:40 字数 472 浏览 3 评论 0原文

我几天来一直在尝试设计这个数据库。我在 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 技术交流群。

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

发布评论

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

评论(4

┊风居住的梦幻卍 2024-12-29 09:14:40

让表members 包含成员数据,表relations 包含成员关系数据。
relations.member_id 将是对成员的引用,relations.lated_member_id - 相关成员的引用。
relations.type 是可枚举的关系类型。

CREATE TABLE `members` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` TEXT NOT NULL ,
`details` TEXT NOT NULL
) ENGINE = INNODB;

CREATE TABLE `relations` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`member_id` INT NOT NULL ,
`related_member_id` INT NOT NULL ,
`type` ENUM(  'Child',  'Parent',  'Friend',  'Spouse' ) NOT NULL,
FOREIGN KEY (member_id) REFERENCES members(id),
FOREIGN KEY (related_member_id) REFERENCES members(id)
) ENGINE = INNODB;

UPD: MyISAM 版本(删除了外键,所有可能的外键功能应由服务器端脚本处理):

CREATE TABLE `members` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` TEXT NOT NULL ,
`details` TEXT NOT NULL
) ENGINE = MyISAM;

CREATE TABLE `relations` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`member_id` INT NOT NULL ,
`related_member_id` INT NOT NULL ,
`type` ENUM(  'Child',  'Parent',  'Friend',  'Spouse' ) NOT NULL
) ENGINE = MyISAM;

Let the table members contain members data and the table relations 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.

CREATE TABLE `members` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` TEXT NOT NULL ,
`details` TEXT NOT NULL
) ENGINE = INNODB;

CREATE TABLE `relations` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`member_id` INT NOT NULL ,
`related_member_id` INT NOT NULL ,
`type` ENUM(  'Child',  'Parent',  'Friend',  'Spouse' ) NOT NULL,
FOREIGN KEY (member_id) REFERENCES members(id),
FOREIGN KEY (related_member_id) REFERENCES members(id)
) ENGINE = INNODB;

UPD: MyISAM version (removed foreign keys, all possible foreign keys functionality should be handled by server side scripts):

CREATE TABLE `members` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`name` TEXT NOT NULL ,
`details` TEXT NOT NULL
) ENGINE = MyISAM;

CREATE TABLE `relations` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`member_id` INT NOT NULL ,
`related_member_id` INT NOT NULL ,
`type` ENUM(  'Child',  'Parent',  'Friend',  'Spouse' ) NOT NULL
) ENGINE = MyISAM;
扎心 2024-12-29 09:14:40

从@Minras设计(+1)开始,我

MEMBERS
  MemberId
  Name
  Details

RELATIONS
  FromMemberId
  ToMemberId
  RelationType

会添加第三个表,而不是检查约束:

RELATIONTYPE
  RelationType
  Description
  FromLabel
  ToLabel

RelationType是整数,“标签”是字符数据。该关系是“定向”的,因为您必须密切注意哪个成员是“来自”,哪个是“到”(但对于“非定向”关系并不那么重要,例如“上过高中”)一起”)。这种设计将允许您:

  • 定义具有多个标签的人之间的关系,例如,A 是 B 的父亲B 是 A 的儿子
  • 如果、何时和必要时添加新的关系

显然,您要么通过外键或任何可用的东西在所有事物上保持关系完整性,要么您将等待发生火车失事。

这并没有解决如何唯一、清晰地识别重名成员的问题。为此,您要么需要考虑现实世界中人们用来处理此类情况的识别属性(学生 ID?社会安全号码?),要么引入特定于您的应用程序的工件(例如,登录名 + 密码) )。

Starting with @Minras design (+1), I’d have

MEMBERS
  MemberId
  Name
  Details

RELATIONS
  FromMemberId
  ToMemberId
  RelationType

But instead of a check constraint, I’d add a third table:

RELATIONTYPE
  RelationType
  Description
  FromLabel
  ToLabel

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:

  • Define a relationship between people with multiple labels, e.g. A is father of B and B is son of A.
  • Add new relationships if, when, and as necessary

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).

︶ ̄淡然 2024-12-29 09:14:40

尝试添加数据透视表:

Relationships:

MemberId1
MemberId2
RelationshipType

Try adding a pivot table:

Relationships:

MemberId1
MemberId2
RelationshipType
烛影斜 2024-12-29 09:14:40

创建一个带有列的表:

Member_1_id
Member_2_id
Relation_type

然后您可以像这样使用它:
如果爱丽丝是鲍勃的女儿,您将拥有这些关系:

<Bob id> <Alice id> 'Father'
<Alice id> <Bob id> 'Daughter'

然后您可以在这些关系中添加一些额外的数据,例如关系何时开始(即某人何时订婚)。

您可能还想在具有成员 ID 的两列上创建索引。

编辑:
为了避免在该表中重复数据,您可以创建一个视图,然后将每个关系仅存储为一行。然而,这个解决方案不允许您命名诸如“父亲”和“儿子”之类的关系。

SELECT member1, member2, relation FROM rel_table UNION ALL SELECT member2, member1, relation FROM rel_table;

Create a table with collumns:

Member_1_id
Member_2_id
Relation_type

Then you can use it like that:
If Alice is Bobs daughter you'll have those relations:

<Bob id> <Alice id> 'Father'
<Alice id> <Bob id> 'Daughter'

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'.

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