需要一些有关 MySQL 模式的输入

发布于 2024-08-18 11:56:47 字数 730 浏览 3 评论 0原文

我需要一些关于如何构建我的数据库的输入。

假设我想将成员数据存储在成员表中。有两种类型的成员,比方说个人和组织。个人和组织具有不同的信息集,因此我们必须将它们存储到两个不同的表中:假设个人和组织。

我将有以下表格:

成员:

id
member_type_id
status
etc..

member_types:

id
type (person|organization|or any other type if needed)

人员:

id
name
etc

组织:

id
name
etc

我的问题是:哪一个是以下最好的?

  1. 在人员和组织表中都有member_id
  2. 在成员表中有一个额外的列来存储person_id或organization_id
  3. 在成员表中有两个额外的列来存储person_id或organization_id(以适用者为准)

还有一个要求是可能有另一个表引用这两个人和组织表。假设我们有一个名为“访问者”的表,该表也可以是个人或组织。

感谢您的意见。

I need some inputs on how I should structure my db.

Let's say I want to store members data in members table. There are two types of members, let's say person and organization. Person and organization have different set of information so that we have to store them into two different tables: let's say persons and organizations.

I would have the following tables:

members:

id
member_type_id
status
etc..

member_types:

id
type (person|organization|or any other type if needed)

persons:

id
name
etc

organization:

id
name
etc

My question is: Which one is the best of the followings?

  1. Have member_id in both persons and organizations tables
  2. Have an extra column in members table to store either person_id or organization_id
  3. Have two extra columns in members table to store person_id or organization_id whichever is applicable

There is another requirement that there may be another table referring to both persons and organizations table. Let's say we have a table called visitors who can also be a person or an organization.

Thank you for your input.

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

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

发布评论

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

评论(2

执妄 2024-08-25 11:56:47

通常最好画出你们的关系,这样更容易理解这两个实体之间的关系。这会让你很容易地说这是一对一、多对一、一对多或多对多等。

话虽如此,一旦你弄清楚了,设置就很简单了。

如果您使用 Java 或 .Net,则可以使用 Hibernate/NHibernate 通过指定关系自动构建表。

以下是我根据您概述的内容设计表格的方法:
(请记住,这是遵循 Java 命名模式)

MemberType
id     (int11, auto increment, primary key)
name   (varchar31, unique)

One note about MemberType, if you're using Java and you will not dynamically add/remove member types, you should use an enum here.


Member
id    (int11, auto increment, primary key)
memberType_id (int11, on update cascade, on delete cascade)


Organization
id    (int11, auto increment, primary key)
name  (varchar31, unique)

-- Depends on how you want this structured (I would design it as a many-to-many relationship
MemberOrganization
member_id       (int11, on update cascade, on delete cascade, this links to the member id)
organization_id (int11, on update cascade, on delete cascade, this links to the organization id)

要获取特定组织的所有成员的列表...

SELECT
 *
FROM
 Organization o
 INNER JOIN MemberOrganization mo ON mo.organization_id = o.id
 INNER JOIN Member m ON m.id = mo.member_id
WHERE
 o.name = 'StackOverFlow.com'

再次查看您的问题后,我认为您正在寻找不同的答案。

沃尔特

It is usually best to draw out your relationships so it is easier to understand how the 2 entities relate. This will let you easily say this is a one-to-one, many-to-one, one-to-many, or many-to-many, etc.

That being said once you figure that out, it is trivial to setup.

If you were using Java or .Net, you could use Hibernate/NHibernate to automatically build the tables for you by specifying the relationships.

Here is how I would design the tables from what you outline:
(Keep in mind, this is following the Java naming schema)

MemberType
id     (int11, auto increment, primary key)
name   (varchar31, unique)

One note about MemberType, if you're using Java and you will not dynamically add/remove member types, you should use an enum here.


Member
id    (int11, auto increment, primary key)
memberType_id (int11, on update cascade, on delete cascade)


Organization
id    (int11, auto increment, primary key)
name  (varchar31, unique)

-- Depends on how you want this structured (I would design it as a many-to-many relationship
MemberOrganization
member_id       (int11, on update cascade, on delete cascade, this links to the member id)
organization_id (int11, on update cascade, on delete cascade, this links to the organization id)

To get a list of all the members of a particular organization ...

SELECT
 *
FROM
 Organization o
 INNER JOIN MemberOrganization mo ON mo.organization_id = o.id
 INNER JOIN Member m ON m.id = mo.member_id
WHERE
 o.name = 'StackOverFlow.com'

After looking at your question again, I think you're looking for a different answer.

Walter

落墨 2024-08-25 11:56:47

您还可以这样做:

会员
ID
姓名
状态

人员(扩展成员)
id(链接到会员 ID)

组织(扩展会员)
访客

ID
ID
日期
member_id(同样,这链接到成员表,您必须执行外部联接才能获取实际的成员数据,无论是个人还是组织)

这使用主键联接列并通过每个子类的单独表进行继承。在 Hibernate 中,查询会自动为您编写/处理,但编写起来并不困难。

让我知道这是否有帮助。

另外,您是否使用 PHP、Java 等,您能否提供更多关于您需要或正在使用它的背景信息?可能有更好的表格设计与此截然不同。

就像我说的,有一些工具可以为您自动执行这些操作,因此您不必担心低级细节。当然,在使用这些工具之前,你需要先了解其中的关系,否则,你的情况不会有任何改善。理解它们固然很好,但这些工具针对您需要的 90% 的内容进行了优化。

沃尔特

You can also do this:

Member
id
name
status

Person (extends Member)
id (links to member id)

Organization (extends Member)
id

Visitor
id
date
member_id (again, this links to the member table, you will have to do an outer join to get the actual member data, either person or organization)

This uses a primary key join column and inheritance through separate table per subclass. In Hibernate, the queries are automatically written / handled for you, but they aren't that difficult to write.

Let me know if that helps.

Also, are you using PHP, Java, etc, and can you provide more background information into what you need or are using this for? There might be a better table design that is very different from this.

Like I said, there are tools out there that automate this stuff for you so you're not worrying about low-level details. Of course, you need to understand the relationship even before using the tools, otherwise, you won't be any better off. It is good to understand them, but the tools are optimized for 90% of the stuff you will need.

Walter

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